Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Integer comparison in byterange value

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have following scenerio. And I want to understand the logic behind it.

If things are in byte range they return true. And if they go beyond then they return false.



Output:



Thanks.
 
Greenhorn
Posts: 29
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we know boxing wont happen just like that....the compiler will place explicit calls....

Integer i = 120; <-----our code

Integer i = Integer.valueOf(120); <--------//compiler generated code

valueOf() method......it is implemented like : if the argument is with in the range of byte, it will make use of existing Object.

if the argument specified is not with in the range of byte it will create a brand new Object...

here
Integer i1 = 120; //create a new Object

Integer i2 = 120; //makes use of the above created Object

Integer i3 = 129; //creates a new Object

Integer i4 = 129; //create new Object

this is what i know....hope it helps
 
Saloon Keeper
Posts: 13493
305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the JVM caches Integer instances within that range, similar to the String pool. So when you assign a literal to an Integer, it will check whether that instance already exists, and use that, instead of creating a new instance.

Outside that range the JVM always creates a new instance.

Regardless, you shouldn't compare two instances using ==, unless checking for identity.
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saikiran is absolutely right.

Here is decompiled code and valueOf() method code:



valueOf code:



Now don't ask why that behavior was given to valueOf method. Following links should help you:

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#190697
https://coderanch.com/t/329148/java/java/Strange-Behaviour-Wrapper-Class
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I see a variable here , what is the value of this variable , also doesn't this behavior varies across JVM , I mean range of caching Integer or it always between - 128 to 127 ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic