• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

output of the code  RSS feed

 
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.
 
Marshal
Posts: 59762
188
  • 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!