成员变量和局部变量的区别
成员变量(Member Variables) 局部变量(Local Variable)
测试局部变量和成员变量在内存中的位置
1.上测试代码
@Service
public class User {
private final static int a1 = 1;
private final static String b1 = "你";
private final static int[] c1 = {1,2,3};
private int d1 = 2;
private String e1 = "好";
private int[] f1 = {4,5,6};
public void aa() {
int d1 = 2;
System.out.println("User的aa()的d1" + " 值 " +d1+ " 内存 " + System.identityHashCode(d1));
String e1 = "好";
System.out.println("User的aa()的e1" + " 值 " +e1+ " 内存 " + System.identityHashCode(e1));
int[] f1 = {4,5,6};
System.out.println("User的aa()的f1" + " 值 " +f1+ " 内存 " + System.identityHashCode(f1));
}
public void bb() {
int d1 = 2;
System.out.println("User的bb()的d1" + " 值 " +d1+ " 内存 " + System.identityHashCode(d1));
String e1 = "好";
System.out.println("User的bb()的e1" + " 值 " +e1+ " 内存 " + System.identityHashCode(e1));
int[] f1 = {4,5,6};
System.out.println("User的bb()的f1" + " 值 " +f1+ " 内存 " + System.identityHashCode(f1));
}
public static int getA1() {
return a1;
}
public static String getB1() {
return b1;
}
public static int[] getC1() {
return c1;
}
public int getD1() {
return d1;
}
public String getE1() {
return e1;
}
public int[] getF1() {
return f1;
}
}
@Service
public class Nat {
private final static int a1 = 1;
private final static String b1 = "你";
private final static int[] c1 = {1,2,3};
private int d1 = 2;
private String e1 = "好";
private int[] f1 = {4,5,6};
public void aa() {
int d1 = 2;
System.out.println("Nat的aa()的d1" + " 值 " +d1+ " 内存 " + System.identityHashCode(d1));
String e1 = "好";
System.out.println("Nat的aa()的e1" + " 值 " +e1+ " 内存 " + System.identityHashCode(e1));
int[] f1 = {4,5,6};
System.out.println("Nat的aa()的f1" + " 值 " +f1+ " 内存 " + System.identityHashCode(f1));
}
public void bb() {
int d1 = 2;
System.out.println("Nat的bb()的d1" + " 值 " +d1+ " 内存 " + System.identityHashCode(d1));
String e1 = "好";
System.out.println("Nat的bb()的e1" + " 值 " +e1+ " 内存 " + System.identityHashCode(e1));
int[] f1 = {4,5,6};
System.out.println("Nat的bb()的f1" + " 值 " +f1+ " 内存 " + System.identityHashCode(f1));
}
public static int getA1() {
return a1;
}
public static String getB1() {
return b1;
}
public static int[] getC1() {
return c1;
}
public int getD1() {
return d1;
}
public String getE1() {
return e1;
}
public int[] getF1() {
return f1;
}
}
@RestController
@RequestMapping("/aaa")
public class Qqq {
@Autowired
private User user;
@Autowired
private Nat nat;
@GetMapping("/bbb")
public void ccc() {
System.out.println("User的a1" + " 值 " +User.getA1()+ " 内存 " + System.identityHashCode(User.getA1()));
System.out.println("User的b1" + " 值 " +User.getB1()+ " 内存 " + System.identityHashCode(User.getB1()));
System.out.println("User的c1" + " 值 " +User.getC1()+ " 内存 " + System.identityHashCode(User.getC1()));
System.out.println("User的d1" + " 值 " +user.getD1()+ " 内存 " + System.identityHashCode(user.getD1()));
System.out.println("User的e1" + " 值 " +user.getE1()+ " 内存 " + System.identityHashCode(user.getE1()));
System.out.println("User的f1" + " 值 " +user.getF1()+ " 内存 " + System.identityHashCode(user.getF1()));
user.aa();
user.bb();
System.out.println("Nat的a1" + " 值 " +Nat.getA1()+ " 内存 " + System.identityHashCode(Nat.getA1()));
System.out.println("Nat的b1" + " 值 " +Nat.getB1()+ " 内存 " + System.identityHashCode(Nat.getB1()));
System.out.println("Nat的c1" + " 值 " +Nat.getC1()+ " 内存 " + System.identityHashCode(Nat.getC1()));
System.out.println("Nat的d1" + " 值 " +nat.getD1()+ " 内存 " + System.identityHashCode(nat.getD1()));
System.out.println("Nat的e1" + " 值 " +nat.getE1()+ " 内存 " + System.identityHashCode(nat.getE1()));
System.out.println("Nat的f1" + " 值 " +nat.getF1()+ " 内存 " + System.identityHashCode(nat.getF1()));
nat.aa();
nat.bb();
}
}
2.运行结果
User的a1 值 1 内存 697815822 Nat的a1 值 1 内存 697815822 User的b1 值 你 内存 1403028226 Nat的b1 值 你 内存 1403028226 User的c1 值 [I@2feff326 内存 804254502 Nat的c1 值 [I@55f2d93a 内存 1441978682 User的d1 值 2 内存 387698249 User的aa()的d1 值 2 内存 387698249 User的bb()的d1 值 2 内存 387698249 User的e1 值 好 内存 225287 User的aa()的e1 值 好 内存 225287 User的bb()的e1 值 好 内存 225287 User的f1 值 [I@576c4b96 内存 1466715030 User的aa()的f1 值 [I@3402bcd0 内存 872594640 User的bb()的f1 值 [I@3ac55420 内存 986010656 Nat的d1 值 2 内存 387698249 Nat的aa()的d1 值 2 内存 387698249 Nat的bb()的d1 值 2 内存 387698249 Nat的e1 值 好 内存 225287 Nat的aa()的e1 值 好 内存 225287 Nat的bb()的e1 值 好 内存 225287 Nat的f1 值 [I@60f718e9 内存 1626806505 Nat的aa()的f1 值 [I@3efad7a0 内存 1056626592 Nat的bb()的f1 值 [I@7bf85e1b 内存 2079874587
3.结果分析
首先声明,我额外试验了,局部变量用final修饰和不用final修饰对指向的值的内存地址没有任何影响.因此上面的结果就没有出现用final修饰的局部变量
内存结果分析就是下面我画的图,都在图里面的
4.对上图结果分析总结
成员变量的内存位置:
1.用final修饰的成员变量,也叫常量.比如final int a=1.这个"a"和"a"指向1的指向,是存在运行时常量池中.
2.没用final修饰的成员变量,比如final int a=1.这个"a"和"a"指向1的指向,是存在方法区的类的信息下面的.
局部变量的内存位置:
1.用final修饰的局部变量,比如int a=1.这个"a"和"a"指向1的指向,百度说是存在栈中的,但是不知道存在栈的哪一块.
2.没用final修饰的成员变量,比如int a=1.这个"a"和"a"指向1的指向,是存在栈的方法自己的栈帧中的局部变量表里的.