• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoboxing occurance question

 
Keith Nagle
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check your private messages (again) for an important administrative matter.

CR
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith:

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


Yep, that's exactly right.
 
Keith Nagle
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know about the format Strings. Try going through the code for the java.util.Formatter class, see whether you can understand it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic