This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Integer CompareTo Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Integer CompareTo" Watch "Integer CompareTo" New topic
Author

Integer CompareTo

Santosh Ramachandrula
Ranch Hand

Joined: Apr 04, 2004
Posts: 252
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)



Thanks,
Santosh
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

I believe one will create an extra object on the heap


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Apr 04, 2004
Posts: 252
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 04, 2004
Posts: 252
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
And yes, the primitive will be boxed, providing you are using Java5 at least.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 04, 2004
Posts: 252
Ok. Thank you!
 
wood burning stoves
 
subject: Integer CompareTo
 
Similar Threads
Integer Constant Pool ????
equals and compareTo method difference?
why compareTo() can be used without implementing Comparale interface
compareTo() and Collections.sort() Solution For Multiple Column Ordering (Ascending) question
Mock Exam doubt Generics