标签:android style blog http java color
//Code: int a = 1000; int b = 1000; //Result: a == b : true
1 //Code: 2 int a = 1000; 3 Integer b = 1000; 4 Integer c = new Integer(1000); 5 6 //Result: 7 a == b:true 8 a == c:true
1 // 简单说,就是比较a、b是否指向的是同一个实例(内存块)。 2 Code: 3 String a = "Test"; 4 String b = "Test"; 5 String c = new String("Test"); 6 String d = new String("Test"); 7 8 Integer x = 1000; 9 Integer y = new Integer(1000); 10 Integer z = new Integer(1000); 11 12 Result: 13 a == b:true 14 a == c:false 15 c == d:false 16 17 x == y:false 18 y == z:false 19 // 上例中,"Test"被放在常量池中,对象a、b的地址均指向该常量。c和d则在堆内重新开辟内存分别存储"Test",所以c和d指向的内存地址也不相同。
1 //Code: 2 int a = 1000; 3 Integer b = 1000; 4 Integer c = new Integer(1000); 5 6 //Result: 7 b.equals(a):true 8 c.equals(a):true
1 //Code: 2 Object a = new String("Test"); 3 Object b = new StringBuilder("Test"); 4 5 //Result: 6 b.equals(a):false
//Code: String a = "Test"; String b = "Test"; String c = new String("Test"); String d = new String("Test"); Integer x = 1000; Integer y = new Integer(1000); Integer z = new Integer(1000); //Result: b.equals(a):true c.equals(a):true c.equals(d):true y.equals(x):true y.equals(z):true
本文系Nodin原创,转载请注明出处!http://www.cnblogs.com/monodin/p/3841219.html
【喵"的Android之路】【番外篇】关于==和equals,布布扣,bubuko.com
【喵"的Android之路】【番外篇】关于==和equals
标签:android style blog http java color
原文地址:http://www.cnblogs.com/monodin/p/3841219.html