aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many Integer Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many Integer Objects" Watch "How many Integer Objects" New topic
Author

How many Integer Objects

Hebert Taype
Greenhorn

Joined: Apr 13, 2008
Posts: 2
Hi i found this a little wierd, please someone could tell me what happen in this code source?

Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("Different Integer objects");

String d1="abc";
String d2="abc";
if (d1==d2){
System.out.println("Same String objects");
}

The result is
"Different Integer objects" and "Same String objects"

The problem is that i know d1 and d2 only reference to one object "abc" and this object is in the pool, but what happen with i1 and i2, shouldn't they have the same behavior as d1 and d2 it means creating only one Integer object 1000 at the pool?...Why do d1 and d2 create 2 different Integer objects?.

Thanks in advance!!!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

well the JVM optimizes the creation String objects which have the same content. But in case of wrapper classes the compiler wraps up the assignment.

So after compilation your code would look as
Integer i1 = new Integer(1000);
Integer i2 = new Integer(1000);

But the String creation would remain the same
String d1="abc";
String d2="abc";


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

and there is no integer constant pool kept by JVM, as far as my knowledge is concerned, so when you create an object like ,Integer i1 and i2, they totally two different object, and objects are never compared with == or != for equality , Why , because this operators are meant for primitive data type and merely checks the reference bit patterns !!

So use Object#equals() method whenever you checks object equality !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1821
    
    7

Originally posted by Sagar Rohankar:
and there is no integer constant pool kept by JVM, as far as my knowledge is concerned, so when you create an object like ,Integer i1 and i2, they totally two different object, and objects are never compared with == or != for equality , Why , because this operators are meant for primitive data type and merely checks the reference bit patterns !!

So use Object#equals() method whenever you checks object equality !



You might want to try running the example in the opening post for Integer values ranging from -128 to 127 (inclusive)


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42594
    
  65
Originally posted by Ankit Garg:
So after compilation your code would look as
Integer i1 = new Integer(1000);
Integer i2 = new Integer(1000);

No. It would amount to this:
Integer i1 = Integer.valueOf(1000);
Integer i2 = Integer.valueOf(1000);
The difference between the two will become clear if you try both with a value of 100 instead of 1000, and then check for object equality.

Originally posted by Sagar Rohankar:
and there is no integer constant pool kept by JVM, as far as my knowledge is concerned, so when you create an object like ,Integer i1 and i2, they totally two different object

Yes and no. The JVM doesn't pool Integer objects, but the Integer class caches them. Try what I described above to see it in action.

objects are never compared with == or != for equality

Objects are always compared with == and != for equality. But there's an important distinction between object equality and value equality. The former is checked with == and !=, while the latter can be checked with the "equals" method.


Ping & DNS - my free Android networking tools app
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Thanks Ulf and Jelle for correcting me !!

but Ulf, I dont understand this ,

Yes and no. The JVM doesn't pool Integer objects, but the Integer class caches them.


Why it caches it, Whats the use?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

I tried the code as
Integer i1 = 100;
Integer i2 = 100;
if(i1 != i2) System.out.println("Different Integer objects");

This makes the objects equal.

I also tried this
Integer i1 = new Integer(100);
Integer i2 = new Integer(100);
if(i1 != i2) System.out.println("Different Integer objects");

This time the objects were unequal......
How does this happen. Ulf Dittmer you are right that it replaces the statement
Integer i1 = 1000;
with
Integer i1 = Integer.valueOf(1000);

The documentation of valueOf method says that it is equivalent to
new Integer(Integer.parseInt(s))

Then how the valueOf passed with an int in the range -128 to 127 return same object for same values....
Or does it has something to do with the == and != operators.

Please tell me...........
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42594
    
  65
Originally posted by Sagar Rohankar:
I dont understand this:
Yes and no. The JVM doesn't pool Integer objects, but the Integer class caches them.
Why it caches it, Whats the use?

Performance. What's the point of creating more than one object for the same integer value? Integer objects are immutable, so once they're created, they can't change their value.

Having said that, there are circumstances where having separate Integer objects for the same value are desirable, and that's when you should use the new Integer(int) constructor. It will always create a new object instead of retrieving an existing one from the cache.

Originally posted by Ankit Garg:
The documentation of valueOf method says that it is equivalent to
new Integer(Integer.parseInt(s))

Actually, it doesn't say that. It says that about the Integer.valueOf(String) method, but here we're using the Integer.valueOf(int) method, and its javadoc mentions caching explicitly.

If you're interested in finding out how the cache works, take a look at the source code of the Integer class. It comes in a file called src.zip which you'll find somewhere in the JDK directory.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

It says that about the Integer.valueOf(String) method, but here we're using the Integer.valueOf(int) method


You are right man...I made a mistake in reading the documentation....As I am getting close to my SCJP exam, I am making new and improved versions of silly mistakes.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many Integer Objects