Skip to main content
added 22 characters in body
Source Link
irfan
  • 896
  • 9
  • 10

An Integer object is immutable, any change in an existing object will create a new object. So after a++, a new object will be created and a will start pointing to that new object while b is still pointing to the old object. Hence, after a++, a and b are pointing to different objects and a == b will always return false.

with respect to the mentioned example :

>Integer a; //created Integer reference
Integer b; //created Integer reference
a = new Integer(2); //created new Integer Object and a reference is assigned to that new object
b = a; //b also start pointing to same Integer object
if(b == a) { // b==a will be true as both are pointing to same object
System.out.println("Strange");
} a++; //after a++ , a new Integer object will be created (due to Integer immutablity and a will point to that new object while b is still pointing to old), so b==a will be false
if(b == a) { System.out.println("Stranger"); }
a--; //again a new Integer Object will be created and now a will start pointing to that new Object , so b==a will be false
if(b == a) {
System.out.println("Strangest");
}`

Integer a; //created Integer reference Integer b; //created Integer reference a = new Integer(2);//created new Integer Object and a reference is assigned to that new object b = a;//b also start pointing to same Integer object if(b == a) { // b==a will be true as both are pointing to same object System.out.println("Strange"); } a++; //after a++ , a new Integer object will be created (due to Integer immutablity and a will point to that new object while b is still pointing to old), so b==a will be false if(b == a) { System.out.println("Stranger"); } a--; //again a new Integer Object will be created and now a will start pointing to that new Object , so b==a will be false if(b == a) { System.out.println("Strangest"); } 

An Integer object is immutable, any change in an existing object will create a new object. So after a++, a new object will be created and a will start pointing to that new object while b is still pointing to the old object. Hence, after a++, a and b are pointing to different objects and a == b will always return false.

with respect to the mentioned example :

>Integer a; //created Integer reference
Integer b; //created Integer reference
a = new Integer(2); //created new Integer Object and a reference is assigned to that new object
b = a; //b also start pointing to same Integer object
if(b == a) { // b==a will be true as both are pointing to same object
System.out.println("Strange");
} a++; //after a++ , a new Integer object will be created (due to Integer immutablity and a will point to that new object while b is still pointing to old), so b==a will be false
if(b == a) { System.out.println("Stranger"); }
a--; //again a new Integer Object will be created and now a will start pointing to that new Object , so b==a will be false
if(b == a) {
System.out.println("Strangest");
}`

An Integer object is immutable, any change in an existing object will create a new object. So after a++, a new object will be created and a will start pointing to that new object while b is still pointing to the old object. Hence, after a++, a and b are pointing to different objects and a == b will always return false.

with respect to the mentioned example :

Integer a; //created Integer reference Integer b; //created Integer reference a = new Integer(2);//created new Integer Object and a reference is assigned to that new object b = a;//b also start pointing to same Integer object if(b == a) { // b==a will be true as both are pointing to same object System.out.println("Strange"); } a++; //after a++ , a new Integer object will be created (due to Integer immutablity and a will point to that new object while b is still pointing to old), so b==a will be false if(b == a) { System.out.println("Stranger"); } a--; //again a new Integer Object will be created and now a will start pointing to that new Object , so b==a will be false if(b == a) { System.out.println("Strangest"); } 
code included for more clarity
Source Link
irfan
  • 896
  • 9
  • 10

An Integer object is immutable, any change in an existing object will create a new object. So after a++, a new object will be created and a will start pointing to that new object while b is still pointing to the old object. Hence, after a++, a and b are pointing to different objects and a == b will always return false.

with respect to the mentioned example :

>Integer a; //created Integer reference
Integer b; //created Integer reference
a = new Integer(2); //created new Integer Object and a reference is assigned to that new object
b = a; //b also start pointing to same Integer object
if(b == a) { // b==a will be true as both are pointing to same object
System.out.println("Strange");
} a++; //after a++ , a new Integer object will be created (due to Integer immutablity and a will point to that new object while b is still pointing to old), so b==a will be false
if(b == a) { System.out.println("Stranger"); }
a--; //again a new Integer Object will be created and now a will start pointing to that new Object , so b==a will be false
if(b == a) {
System.out.println("Strangest");
}`

An Integer object is immutable, any change in an existing object will create a new object. So after a++, a new object will be created and a will start pointing to that new object while b is still pointing to the old object. Hence, after a++, a and b are pointing to different objects and a == b will always return false.

An Integer object is immutable, any change in an existing object will create a new object. So after a++, a new object will be created and a will start pointing to that new object while b is still pointing to the old object. Hence, after a++, a and b are pointing to different objects and a == b will always return false.

with respect to the mentioned example :

>Integer a; //created Integer reference
Integer b; //created Integer reference
a = new Integer(2); //created new Integer Object and a reference is assigned to that new object
b = a; //b also start pointing to same Integer object
if(b == a) { // b==a will be true as both are pointing to same object
System.out.println("Strange");
} a++; //after a++ , a new Integer object will be created (due to Integer immutablity and a will point to that new object while b is still pointing to old), so b==a will be false
if(b == a) { System.out.println("Stranger"); }
a--; //again a new Integer Object will be created and now a will start pointing to that new Object , so b==a will be false
if(b == a) {
System.out.println("Strangest");
}`

Tweak formatting
Source Link
anothernode
  • 5.4k
  • 14
  • 48
  • 66

An Integer object is immutable  , any change in an existing object will create a new object so. So after a++a++, a new object will be created and 'a'a will start pointing to that new object while bb is still pointing to the old object hence. Hence, after a++ a++, aa and bb are pointing to different objects , and a==ba == b will always return falsefalse.

Integer object is immutable  , any change in existing object will create a new object so after a++, new object will be created and 'a' will start pointing to that new object while b is still pointing to old object hence after a++ , a and b are pointing to different objects , and a==b will always return false.

An Integer object is immutable, any change in an existing object will create a new object. So after a++, a new object will be created and a will start pointing to that new object while b is still pointing to the old object. Hence, after a++, a and b are pointing to different objects and a == b will always return false.

Source Link
irfan
  • 896
  • 9
  • 10
Loading