Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer CompareTo

 
Santosh Ramachandrula
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • 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)

 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe one will create an extra object on the heap
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Ok. Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic