aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP 1.5  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 "SCJP 1.5  "In order to save memory ??"  QUOTE  " Watch "SCJP 1.5  "In order to save memory ??"  QUOTE  " New topic
Author

SCJP 1.5 "In order to save memory ??" QUOTE

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Hi,

In Chapter 3, p236 of K & B SCJP 1.5 Certification Text has the following excerpt. Can you tell me how the JVM saves memory with this feature implementation? Thanks.



Produces the output:
different objects
meaningfully equal




This example produces the output:
same object
meaningfully equal

Yikes! The equals() method seems to be working, but what happened with ==
and != ? Why is != telling us that i1 and i2 are different objects, when == is saying
that i3 and i4 are the same object? In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:
-- Boolean
-- Byte
-- Character from \u0000 to \u007f (7f is 127 in decimal)
-- Short and Integer from -128 to 127


From K & B SCJP Certification Text Errata, the following is abstracted:


236....clarify...Sentence before bullet points s/b: In order to save memory, two instances of the following wrapper objects, created through autoboxing, will always be equal...
Brandon Bernie
Greenhorn

Joined: Nov 02, 2007
Posts: 9
It looks like K & B have answered your question in your quoted text. What are you unclear about?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Because they're the same objects there's only one object instead of two.
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
How can i3 and i4 be the same object when I instantiated them separately? i3 is a reference to one object, and i4 is a reference to a different object. They are meaningfully equal. I think I see the reason now, but it isn't that obvious. After incrementing i3, it points to a different wrapper object.

class NotEqualObjects{
public static void main(String [] args)
{
Integer i3 = 10;
Integer i4 = 10;
System.out.println("i3 = " + i3);
System.out.println("i4 = " + i4);
i3++;
System.out.println("i3 = " + i3);
System.out.println("i4 = " + i4);
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

How can i3 and i4 be the same object when I instantiated them separately? i3 is a reference to one object, and i4 is a reference to a different object. They are meaningfully equal.


But you did *not* instantiate i3 and i4. You declared i3 and i4. And you used autoboxing to "create" the object. Autoboxing uses the valueOf() method, which may instantiate a new object, or may get you a copy from an internal cache.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Srinivasa Chebrolu
Greenhorn

Joined: May 29, 2009
Posts: 4
-- Short and Integer from -128 to 127


i3 & i4 assigned values fall with in -128 - 127 and their primitive values same hence i3 == i4.
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Srinivasa,

i3 is a reference to an Integer object, not an int primitive.
i4 is also a reference to an Integer object, not an int primitive.

Integer is a wrapper class.

i3 and i4 are references to the same object, an Integer wrapper object that wraps a primitive with a value of 10. Because i3 and i4 are referencing the same object, the JVM saves memory. Because i3 and i4 are referencing the same object, i3==i4.

Harry
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Autoboxing works like this: If you write a line such as:

The compiler will automatically convert this to:

Now, the implementation of class Integer contains something smart to avoid making a lot of Integer objects for often used values. Class Integer has a cache of Integer objects, which represent all the values between -128 and 127. When you call the valueOf() method, it first looks if the value that you pass in is between -128 and 127. If it is, then it returns an Integer object from the cache; otherwise, it creates a new Integer object with the value you specified.

If you do something like this:

This will be converted to:

In both these lines, valueOf() will return the same Integer object from its cache. If you compare i3 with i4 by using ==, you will get 'true', because they refer to the same Integer object.

If you would use a value outside the range -128 to 127 instead of 10, the result would be 'false'.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Also, note that we have a separate SCJP forum. I will move this topic there for you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP 1.5 "In order to save memory ??" QUOTE