• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper Class Instantiation

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any underlying difference between following two lines?

Integer i = 10;

and

Integer i = new Integer(10);

Suppose I write

Integer i1 = 10;
Integer i2 = 10;

then (i1==i2) test returns true. But if say

Integer i1 = new Integer(10);
Integer i2 = new Integer(10);

then (i1==i2) test returns false.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to save memory, two instances of the following wrapper objects will always == when their primitive values are the same:
Boolean
Bye
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127

Hope this help you
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will check in the Pool for the Availability of the Object
Here i2 will get it as i1

so
(i1==i2) will return try

But

Here We are calling Explicitly call new constructor so It will not look in the pool so it return false.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rubayat,

When you write:

Integer i1 =10;

Before Java 5.0 it was compile error to do so. You couldn't have assigned an int to Integer reference. But thanks to Autoboxing, primitive int is automatically boxed to Integer.

The compiler does the job for you: Internally it does:
Integer i1 = Integer(10);

But to save memory when the range of int is withing 127 (max range of byte), comparison of two references that are created with the following way
always equal
Integer i1 = 10;
Integer i2 = 10;
if(i1==i2) {
System.out.println("==");
}

When you create an a wrapper class object using new operator a new object is created an assigned to the wrapper reference. So each time you create an
object using new, new object is created on the heap. So the references will point to different objects regardless of the encapsulating primitive. Consequently your comparison to two references results false even if both refer to the objects encapsulating equal primitive, in your case it is 10.

I hope this helps you.

Regards,
CMBHATT
[ March 07, 2007: Message edited by: Chandra Bhatt ]
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic