• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Equal Test  RSS feed

 
Anjali Pal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All,

I was just kidding around with some core concepts of java in my spare time , very small nothing too big at all ,

Strings and Objects as a whole are mutable in java and "==" should not work on them , that is in theory that is.

But every time i write a code like this

public class MutableString {

public static void main(String[] args) {


String i = "hi";
String j = "hi";

if(i==j)
{
System.out.println("The Strings are equal !!!");
}
else
{
System.out.println("The Strings are NOT equal !!!");
}
}

}

The if part executes !!!
I get the Output : The Strings are equal !!! .

How is this happening its amazing i want to know what happens behind the seen?
How can one do see what is going on ?

Even more amazing is when i do this !!!

public class MutableString {

public static void main(String[] args) {


Integer i = 100;
Integer j = 100;

if(i==j)
{
System.out.println("The Integers are equal !!!");
}
else
{
System.out.println("The Integers are NOT equal !!!");
}
}

}
And again the if part executes !!!
I get the Output : The Integers are equal !!! .

But this is a consistent behavior with the first "String" example program. Right!!!

Now the magical part , I change the value of variable i and j to 1000 as given below

public class MutableString {

public static void main(String[] args) {


Integer i = 1000;
Integer j = 1000;

if(i==j)
{
System.out.println("The Integers are equal !!!");
}
else
{
System.out.println("The Integers are NOT equal !!!");
}
}

}

and guess what i get the else part as an output : The Integers are NOT equal !!!

Please experts let this misery be unfold to me

Regards,
Anjali

 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The if part executes !!!
I get the Output : The Strings are equal !!! .

How is this happening its amazing i want to know what happens behind the seen?
How can one do see what is going on ?


To cut down the number of String objects created in the JVM, the String class maintains a pool of strings. Each time we create a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns.

so when you say String j="hi"

hi is already existing in the string pool, and thus its the same reference what i had and thats why i==j evaluates to true.
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Integer i = 100;
Integer j = 100;

Integer i = 1000;
Integer j = 1000;


In both the above cases auto boxing is happening. The Integer class handles auto boxing in two different ways i.e. if the value is >=-128 and <=127, it uses a cached instance or else it creates a new instance. But probably the values differs from jvm to jvm.

So for the first case i.e. j=100 will use the cached instance the resulting i==j to true. But in the later case j=1000 a new instance will be created.
 
Anjali Pal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for the response but then dose not this defeats the whole purpose of objects being immutable ?

Regards,

Anjali
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, and as per JLS String itself is immutable, and not very sure but probably for the wrapper classes it has been done for faster access.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:But probably the values differs from jvm to jvm.


The values that must be cached (-128 to 127) are the same on all JVMs. So this will always print true

However, there is no restriction on a JVM caching values outside this range, so you should never rely on the following to print false. It would be perfectly legal for it to print true.

 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I did not know this caching fundamental in case of Integers. Is it true with other classes? If yes, please mention those also or please provide the doc from where i may increase my stuff on this.

I am curious to know - Why is it that JVM allows (-128 to 127) range only?

Thanks a lot.

Patricia
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it that JVM allows (-128 to 127) range only?


This is not the only range. JVM must allow this range, however there is no restriction on jvm caching values higher than this range. See the post written by Joanne Neal.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Samuel wrote:please mention those also or please provide the doc from where i may increase my stuff on this.


The Java Language Specification Enjoy
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote: Enjoy
Joanne! How could you!
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!