• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Difference

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody please tell me is there is any difference if we create primitive object by these two ways
Integer i1= 14;
Integer i2= new Integer (14);


and also explain this two line code also




 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please choose an appropriate title for your question that explains what problem you are facing.



15 is a primitive that is autoboxed to an Integer object. Behind the scenes however, the compiler does not invoke new Integer(15) but Integer.valueOf(15).

The difference between the two is that the valueOf method works with an internal cache that ranges from -127 - 128, hence two objects created with valueOf that are in that range will pass the == test.

new Integer ALWAYS instantiates a new object, regardless if the number is 1, 5 or 123131.
 
vineet walia
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please
any more
 
vineet walia
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian Janisch wrote:Please choose an appropriate title for your question that explains what problem you are facing.



15 is a primitive that is autoboxed to an Integer object. Behind the scenes however, the compiler does not invoke new Integer(15) but Integer.valueOf(15).

The difference between the two is that the valueOf method works with an internal cache that ranges from -127 - 128, hence two objects created with valueOf that are in that range will pass the == test.

new Integer ALWAYS instantiates a new object, regardless if the number is 1, 5 or 123131.




// false
can you explain why its not true this time
 
author
Posts: 23937
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

// false
can you explain why its not true this time



Only the valueOf() method, that takes an int, and used by autoboxing, uses the integer cache.

Henry
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are giving it a String and not a primitive int, hence another mechanism of creating the Integer object is in force.

Use Integer.parseInt() for Strings
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you create an Integer object through auto-boxing they are stored in a special place in the heap.......

just like in the case of String objects created using double quotes("").....

the following are the data-types and their ranges which when created through auto-boxing with same type and value will point to same object on heap......

1)Boolean
2)Byte
3)Short and Integer from -128 to 127
4)Character....

 
vineet walia
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Sebastian Janisch and Henry Wong
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vineet walia wrote:Anybody please tell me is there is any difference if we create primitive object by these two ways
Integer i1= 14;
Integer i2= new Integer (14);


and also explain this two line code also






Also remember that when comparing two objects you should use the .equals() method when comparing value equality. So try the following:
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The range is actually -128 to 127. If the value falls outside the range valueOf doesnt work..rather new objects are created..making them unequal.
HTH
 
Henry Wong
author
Posts: 23937
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The range is actually -128 to 127. If the value it fall outside the range valueOf doesnt work..rather new objects are created..making them unequal.



This is a nitpick... The java specification merely states the range that must be cached. It does *not* state what happens to values that fall out of the range. It is perfectly valid to have a JVM implementation, that caches a larger range, and hence, have the same object after autoboxing.

Henry
 
I am Arthur, King of the Britons. And this is a tiny ad:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic