• 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

Integer CompareTo

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Will the behavior of these two statements be same or different? (both compile and run but want to know if their behavior is same at all times)

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe one will create an extra object on the heap
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other version might create a new object too, if that value has not already been cached. The documentation for Integer#valueOf(int) doesn’t say whether the caching occurs in advance of the first use of a particular value, or when that value is first used.
 
Santosh Ramachandrula
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you are saying that "statement 2" will create an extra object in the heap but the two statements will always return the same value all the time, right?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Santosh Ramachandrula wrote:So you are saying that "statement 2" will create an extra object in the heap



The code will not compile. However, if you change it so that it will, then the arg to compareTo() statement 1 will always create a new Integer object, and the arg to compareTo() in statement 2 might or might not create a new Integer object.

but the two statements will always return the same value all the time, right?



Yes, which you can tell by reading the documentation for Integer.compareTo()
 
Santosh Ramachandrula
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies, I updated the statements in question with a new Integer(...) in both statements.

When I pass a primitive "int"in the second statement "does Java convert it to Integer object by "auto boxing"?

Following is the source code from Java 6 documentation.

/>
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The () have confused the site software, so I edited your post by moving the link into [url][/url] tags. Better to use the buttons than write tags by hand.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Santosh Ramachandrula originally wrote:Hi Will the behavior of these two statements be same or different? (both compile and run but want to know if their behavior is same at all times)

It would have been better not to alter the original post after it has been replied to.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Santosh Ramachandrula wrote:My apologies,

Apology accepted

Following is the source code from Java 6. . .

It is usually better to link to the API: Jeff Verdegan has a link in his most recent post before this one.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And yes, the primitive will be boxed, providing you are using Java5 at least.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Santosh Ramachandrula wrote:
When I pass a primitive "int"in the second statement "does Java convert it to Integer object by "auto boxing"?



Your questions will be clearer if all the relevant details are together, rather than having to refer back 5 or 10 posts so see what you're talking about.

Are you asking if the 2 is autoboxed in


?

If so, the answer is yes. Anywhere an Integer is expected and you provide a primitive int, if it compiles, it's autoboxing. And anywhere a primitive int is expected and you provide an Integer, if it compiles, it auto-unboxing.

 
Santosh Ramachandrula
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Thank you!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic