• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Help-wrapper classes

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I have been reading about wrapper classes.
I have a doubt.

Integer i=new Integer(2);line 1
Integer j=Integer.valueOf(2);line 2
Integer i=2;line 3

Line 1 - It creates a new Integer object
Line 2 - The method returns a newly created integer object
Line 3 - what does this line do.Object creation?

Help me in understanding this.

Thanks
Praveen Sp
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Line 3
Integer i=2 also creates a new Object.
But there is a difference between
Integer i=2 //Line 1
and Integer i=new Integer(2); //Line 2

How?
Here is an Example to Illustrate it:-

Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
if(i1 != i2) System.out.println("different objects");

This would always Print "different objects" since new Object is created everytime you use new Keyword.

But if you do it like this,
Integer i1 = 2;
Integer i2 = 2;
if(i1 != i2) System.out.println("different objects");

Both i1 and i2 refers to same Object !.So it wont Print "different objects".


However it is subject to some conditions,
two instances of the
wrapper objects will refer to same Object when their primitive values are the same and each of these is :
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi pooja

why does this happen/

Integer i1=new Integer(2);
Integer i2=new Integer(2);
here (i1!=i2)
Integer i1=2;
Integer i2=2;
here (i1==i2)


If both types create Integer wrapper object,why is this strange behaviour.

Thanks
Praveen SP
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Praveen Seluka:
Integer i=2;line 3
Line 3 - what does this line do.Object creation?


An Integer object is created automatically by autoboxing.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic