Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

output of the code  RSS feed

 
nishi kishor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
i have a code like this

Integer i1 = 128;
Integer i2 = 128;
System.out.println(i1 == i2);
Integer i3 = 127;
Integer i4 = 127;
System.out.println(i3 == i4);

The output of above code :
false
true

please tell me that how this output come as this.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You appear to have arrived in the wrong forum, but don't worry; I can move your question.

Have a look at the documentation for the Integer#valueOf method, and the JLS (=Java┬« Language Specification) about boxing conversions. Look particularly at what it says about values ≥ −128 and ≤ 127. That should give you a hint why you are getting strange results from the == operator. Another reason not to use ==.
If you can't work out the problem from those resources, tell us, and we shall drop some more hints.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!