aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another printf question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "another printf question" Watch "another printf question" New topic
Author

another printf question

Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
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

Joined: Feb 24, 2006
Posts: 6662
    
    5

The toString() method is inserted automatically by the compiler without the need for the programmer to code it in. Thats why it compiled.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Atilla Ilhan
Greenhorn

Joined: May 13, 2009
Posts: 7
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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another printf question