• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object comparasion question

 
Garg Amit
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain why the output is false.

public class CheckNeavigableCode {


public static void main(String args[]) {
Integer m = Integer.parseInt("-256"); // When I treid with -1,127,0 I got true as result.
Integer n = m;
m--;
m++;
System.out.println(m == n);
}

}
 
Devaka Cooray
Marshal
Posts: 5216
511
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garg Amit,

It is because of the pooling feature of Java. For numerical values only values within the range -128 to 127 could be pooled to be reused. Hence, two Integer objects may have the same identity in the JVM in this case.

But when the value is out of the above range, general Java object identity comparisons could be applied, so two Integer OBJECTS have different identities.

Try the below program:




This forum is for Sun Certification Results. When posting again, please use an appropriate forum.
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!