• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A small help needed about Wrapper Class

 
Vishram Khare
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I am using JCreator as an IDE for my java programs with classpath pointing to jdk1.6.0_02 folder.That means I am using JDK 6.

IN K&B's book for exam SCJP 5, on page 236 it is mentioned that the two DIFFERENT instances of Integer wrapper class will always yield true to == comparison when the undelying values of the two class objects are same and between -128 and 127. I coded the dollowing java program by combining the code given on pages 235 and 236 of K&B's book.


public class WrapperDemo2{

public static void main(String[] args){
WrapperDemo2 w=new WrapperDemo2();
Integer i1=new Integer(1000);
Integer i2=new Integer(1000);

Integer i3=new Integer(10);
Integer i4=new Integer(10);

if(i1!=i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningully equal");

if(i3==i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");

}
}

On Page 235,it is mentioned that for first two if statements,we should get the following output---

different objects
meaningully equal

On Page 236,it is mentioned that next two if statements produce output--

same object
meaningully equal

However I am getting the following output when I run the program--

different objects
meaningully equal
meaningfully equal

That means third if condition is false and hence that loop is not executed.

Could somebody throw some light on this?I am using JDK 6 version.Is it possible that JDK 5 will give one type of behavior and JDK 6 opposite?

Thanks

----Girish
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Girish Khare:
IN K&B's book for exam SCJP 5, on page 236 it is mentioned that the two DIFFERENT instances of Integer wrapper class will always yield true to == comparison when the undelying values of the two class objects are same and between -128 and 127.

But only if you create those two Integer objects by autoboxing, or by calling Integer.valueOf(...). If you are explicitly creating two Integer objects with 'new Integer(...)', then == between those two objects will return false, because they are two different objects.

The reason for this is the following: The method Integer.valueOf(...) has an optimization to avoid creating many Integer objects. It has a pool of Integer objects for all the values between -128 and 127. If you call Integer.valueOf(...) with a value in that range, it will return the corresponding Integer object from the pool instead of creating a new object. So if you call it twice, you'll get the same object from the pool twice, and == will be true between those.

When you use autoboxing, the compiler translates it behind the scenes to a call to Integer.valueOf(...). So autoboxing changes this:

Integer a = 10;

into this:

Integer a = Integer.valueOf(10);

It is always a good idea to use Integer.valueOf(...) instead of new Integer(...), because the first makes it possible to take advantage of the optimization.
[ September 25, 2007: Message edited by: Jesper Young ]
 
Vishram Khare
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Jesper.I made the change and got it working.

--Girish
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic