File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another printf question

 
Higgledy Smith
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("%s", new Long("123") );

I thought this would not run but it did. How is the long 123 being converted to a String? Is this autoboxed to Long > Object > String?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The toString() method is inserted automatically by the compiler without the need for the programmer to code it in. Thats why it compiled.
 
Atilla Ilhan
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Higgledy Smith wrote:System.out.printf("%s", new Long("123") );

I thought this would not run but it did. How is the long 123 being converted to a String? Is this autoboxed to Long > Object > String?


can someone explain this a little bit more.. why toString() method inserted automatically? while we want to create a new object of type Long,
it has a string argument but new Long(123) compiles also
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure about the technical validity of that statement -> either the compiler inserting the toString(); or the JVM interpreting the statement as an implicit call to toString(). However the answer to your other question lies here

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Long.html#Long(long)
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is not done by the compiler... the toString() method is called by the printf() method. Here is the relevant quote in the JavaDocs that explains what happens with the "%s" format type.

Conversion Argument Category Description

's', 'S' general If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().


Henry
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

look at the format string description in the api: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

Conversion char : 's', 'S'
Description : If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().


new Long("123") isn't null nor implements Formattable, so during conversion it's toString() method is invoked.
And toString() invocation returns "123".
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that this has nothing to do with autoboxing. Autoboxing is the automatic conversion of a primitive type to the corresponding wrapper type (long to Long, int to Integer, etc.).

In your example above, there is no value of a primitive type, so there is also no autoboxing.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic