0

What is difference between following?

Integer i1=new Integer(10);
Integer i2=new Integer(10);

System.out.println(i1==i2) //output is false //means that i1 and i2 does not have same refernce but when i do

Integer i1=10;
Integer i2=10;

System.out.println(i1==i2) //output is true //means that i1 and i2 have same reference why is it so?

Patel Parth
  • 315
  • 5
  • 16
  • This discussion might help, http://stackoverflow.com/questions/12306853/behavior-of-wrapper-class-in-java and https://www.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching – kosa Mar 08 '16 at 21:24
  • Integer values between -128, 127 are cached and since you are not using keyword new, references point to the same cached object. – John Mar 08 '16 at 21:25

0 Answers0