Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

query with Wrapper classes...  RSS feed

 
suresh sargar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");


This example produces the output:
same object
meaningfully equal

but when i tried i3=i4=1000 (greter than 127 ) i3 & i4 are not equal.can anyone tell me how its works..?
 
Rob Spoor
Sheriff
Posts: 21114
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Byte, Short, Integer and Long, values between -128 and 127 (inclusive) are cached. The valueOf methods, which are called automatically by autoboxing, will return these cached values, and for all other numbers return a new object.

Character only caches values between 0 and 127 (inclusive).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!