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

are these one and the same

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
is
Integer i=100; equivalent to

Integer i=new Integer(100);
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Vishal Hegde wrote:is
Integer i=100; equivalent to

Integer i=new Integer(100);



No, auto-boxing is like calling Integer i = Integer.valueOf(100); The difference is that there are a range of values (-128 to 128 or something like that) which are cached, so auto-boxing and the valueOf method will be quicker/more memory efficient for low int values then creating a new Integer.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
THEn what is Integer i=100 if its not auto boxing?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Vishal Hegde wrote:THEn what is Integer i=100 if its not auto boxing?



Integer i = 100; is auto boxing, and auto boxing has the affect of calling Integer i = Integer.valueOf(100);
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Vishal Hegde wrote:THEn what is Integer i=100 if its not auto boxing?



For values from -128 to 128 compiler creates 'constant' integer objects in memory and refers to them.
Look at this example:

Result will be:
i == j
x != y

For values less than -128 and grather thar 128 you will get:
i != j
x != y

But except this difference the i =100 and i = new Integer(100) are equivalent.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
ohhk basically it means that when we write

Integer i1=100
Integer i2=100 // it must be within -128 to 128
and thus if the instance has same value then they are pointing to the same object and thus equal

on the other hand
Integer i1=new Integer(100);
Integer i2=new Integer(100); // in this case both referece variable i1 and i2 are different objects

Am i right??


 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
See http://faq.javaranch.com/java/JavaIntermediateFaq#integerAutoBoxing
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Closing because this appears to be a duplicate of this thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic