AutoBoxing,==,!=,equals()

AutoBoxing,==,!=,equals()

Hi,
I am really confused of ==,!=,equals() operations on Autoboxing the new feature in java 5.0 .
Can any one tell me,how the above operations are performed on Boxing?
See the below examples. They should print the same outputs in case of == or != . But in Example1 it says there are different objects and in Example2 it prints Same Objects. Why is it so? If any knows please reply to this.

Example 1:
==========
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");

Produces the output:
--------------------
different objects
meaningfully equal

Example 2:
==========
Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");

Produces the output:
--------------------
same object
meaningfully equal

Thanks.
View Answers

February 24, 2010 at 8:26 AM

Java language have 2 data types .
1.Primitive type.
2. Reference type.

Primitive values do not share state with other primitive values. A variable whose type is a primitive type always holds a primitive value of that same type. The value of a variable of primitive type can be changed only by assignment operations on that variable.

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

According to your first example
Integer i1 and i2 means two different objects and their memory addresses are different.But they are point same value.That 's why
it print 'different objects'

for comparing object values we use ob1.equals(object ) method which return
true if the comparing two object have same value ,that ' s why it print
'meaningfully equal'
---------------------------------------------------------------------------For your second question i take this from
http://www.theserverside.com/news/thread.tss?thread_id=27129


I've seen a lot of posts in this thread about what is happening on ==

It's simple:

When we do
Integer i = 127;
autoboxing turns this into:
Integer i = Integer.valueOf( 127 );

Go look at the implementation of Integer.valueOf(), and you'll find:
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}

If the int value is between -128 and 127, it will return a cached Integer value. This way we avoid creating many duplicate Integer objects for those common values. It save's on memory and improves performance.

And, of course, it does mean that Integer i = 127 will always return the same Integer instance.

--------------------------------------------------------------------------




For more on this topic please visit following urls.
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#85587

http://creativekarma.com/ee.php/weblog/comments/value_types_in_java/

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

Hope this helpful.
Best Regards.




February 24, 2010 at 6:54 PM

Thanks a lot.

Yah even I was shocked just as others in the thread of serverside.com when I tested '==' for the range of -128 to 127 and above b4 posting this question.

But now, no more wonders. I get it all.. Thank you very much for the post.
The thread in serverside.com helped a lot to understand this.

February 4, 2012 at 8:38 PM

1) As in real world two objects(two persons) can not be same even though their attribute's values are same,both person have unique identity in real world.Similarly in two objects can not be same if they have their value is same.In java both objects have their own memory and addresses,== operator check only reference means memory address of object that means they are unique to each other.

2)equals method has been overridden in Integer class which convert the object(Integer) value in primitive(int) and checks whaethe the value are same or not,in this case it is same.









Related Tutorials/Questions & Answers:

Ads