Thursday, March 17, 2016

[Java] "==" for String and integer

Q1 :
                String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
System.out.println(s1 == s2);   //true (const pool)
System.out.println(s1 == s3);  //false (address)

Q2 :
                Integer a = new Integer(1000);
int b = 1000;
Integer c = new Integer(10);
Integer d = new Integer(10);
System.out.println(a == b); //true! (not same address, but b is int, here == will compare the value not the address)
System.out.println(c == d); //false (== for address , new object in heap, not use const pool)

No comments:

Post a Comment