Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
    Bookmark Topic Watch Topic
  • New Topic

Strange Behaviour Of Wrapper Class

 
Sanket Meghani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello EveryOne,

I was just playing around the wrapper classes and observed some unexpected behaviour.

public class HelloWorld
{
public static void main( String args[] )
{
Integer i1 = 100;
Integer i2 = 100;

System.out.println( "i1 == i2 : " + ( i1 == i2 ) );
}
}

Output of the above code is unexpected with different values of i1 and i2.

For the current values ( i1 = 100 and i2 = 100 ) the output is : "i1 == i2 : true"

But for the following code,

public class HelloWorld
{
public static void main( String args[] )
{
Integer i1 = 1000;
Integer i2 = 1000;

System.out.println( "i1 == i2 : " + ( i1 == i2 ) );
}
}

the output is "i1 == i2 : false"

From different conbinations I have observed that in the above code for values of i1 and i2 between -128 to 127 ( range of byte ), i1 == i2 evaluates to true and for other values i1 == i2 evaluates to false.

What could be the reason behind this behaviour?

Thank You,
Sanket Meghani.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Report post to moderator
Check out this post.

Specifically the discussion that follows on from the first paragraph in Ken Blair's first post.
[ August 03, 2007: Message edited by: Joanne Neal ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Report post to moderator
Cross-post. Also posted in Advanced, and answered there (initially by me).
 
    Bookmark Topic Watch Topic
  • New Topic