直接上图:
猜测:为什么a==c
1.因为b是final修饰,所以b属于最终变量,此处可以理解为常量,因为String采用的是和八种基本类型一直的传参方式,即值传递,又被final修饰称为常量,所以b在使用的时候,采用的是直接替换,a中的"hello"就是b。a相当于b+“2”;变向输出内存地址的方法:System.identityHashCode(a)
参考代码:
public static void main(String[] args) {
String a = "hello2";
final String b = "hello";
final String bb = "2";
String d = "hello";
String c = b + 2;
String e = d + 2;
String ccc = b + "2";
String cc = "hello" + bb;
String ee = "hello" + "2";
String ff = b + bb;
System.out.println(ee == ff);
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(ccc));
System.out.println(System.identityHashCode(c));
System.out.println(System.identityHashCode(e));
System.out.println(System.identityHashCode(b));
System.out.println(System.identityHashCode(2));
System.out.println(System.identityHashCode("2"));
System.out.println(System.identityHashCode("hello"));
System.out.println((a == c));
System.out.println(a.getClass().hashCode());
System.out.println(c.getClass().hashCode());
System.out.println(e.getClass().hashCode());
System.out.println((a == cc));
System.out.println(a.equals(c));
System.out.println(a.equals(e));
System.out.println((a == e));
System.out.println(c);
System.out.println(e);
}
原版出处:http://www.cnblogs.com/dolphin0520/p/3736238.html