java - Comparing two Integer Wrapper classes -


why program printing false in first print statement , true in print statement.? , i1 2 different objects first statement must print "true", expected, second print statement prints "false" creates confusion.

public static void main(string[] args) {         integer = new integer(10);         integer i1 = new integer(10);         system.out.println(i == i1); //printing false         i++;         i1++;         system.out.println(i == i1);//printing true     }  

using new keyword always creates 2 different instances. following true:

new integer(10) != new integer(10) 

hence first line printing "false".

then:

i++; 

hides unboxing , boxing. equivalent to:

i = integer.valueof(i.intvalue() + 1); 

as described in the javadoc of integer.valueof, values -128 127 (at least) cached: getting cached instance of integer.valueof(11) both i++ , i1++, hence second line printing "true".


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -