wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Autoboxing occurance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Autoboxing occurance question" Watch "Autoboxing occurance question" New topic
Author

Autoboxing occurance question

Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Hi there.
Suppose you have
Integer x = 343;
long L343 = 343L;
if(x.equals(L343))
if(x.equals(343))

what happens in the 2 above statements?
Would I be right in saying that the L343 and
343 will be auto-boxed?
Thank you!

Profile updated
[ June 18, 2008: Message edited by: Keith Nixon ]

SCJP 5.0
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Please check your private messages (again) for an important administrative matter.

CR
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Keith:

Would I be right in saying that the L343 and
343 will be auto-boxed?


Yep, that's exactly right.


[Jess in Action][AskingGoodQuestions]
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Much obliged.
Thanks & best regards.

Actually, just a follow up question, in relation to
string formatting.
If I have:
System.out.printf("%s", new Integer("123"));
a string is expected from new Integer("123")
so what happens here? Why does this run?
Is Integers instance method toString() called?
Thank you again.
[ June 18, 2008: Message edited by: Keith Nixon ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Thank you for correcting your name. One of the numbers will be autoboxed to an Integer the other to a Long.

Look for the Integer class constructor, where you can find the details; it says the same way as parseInt(). Go into your Java installation folder; you will find a file called src.zip or similar. Unzip that, go into the java folder, then lang, then the Integer class, and you can find the actual code of the constructor and see exactly how it does it.
[ June 18, 2008: Message edited by: Campbell Ritchie ]
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Originally posted by Campbell Ritchie:
Thank you for correcting your name. One of the numbers will be autoboxed to an Integer the other to a Long.

Look for the Integer class constructor, where you can find the details; it says the same way as parseInt(). Go into your Java installation folder; you will find a file called src.zip or similar. Unzip that, go into the java folder, then lang, then the Integer class, and you can find the actual code of the constructor and see exactly how it does it.

[ June 18, 2008: Message edited by: Campbell Ritchie ]

Thanks Ritchie.
Im cool with the constructor of the Integer class but im curious
about the above printf method because the conversion character is a
string and in that case, Im wondering if the toString() method is called on the Integer instance?
Thanks again & best regards.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Don't know about the format Strings. Try going through the code for the java.util.Formatter class, see whether you can understand it!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Autoboxing occurance question
 
Similar Threads
String doubts from K&b Self Test
Queries--
comparison doubt - K&B book
Questios having in correct answers
Integer.equals() method