• 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
Pie
Posts: 4889
407
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic