• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Object Equal Test

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 75861
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic