Query from Kathy Sierra book
In chapter 6 self test question no 9 :
System.out.format("%s", new Long("123"));
This code is said to be fine and that it will compile without any error.
Here we are creating and object of the Long wraper class and %s is for String. In the previous chapters we studied that Wrapper classes cant be widened among one another.
So can any tell me the concept why this piece of code will compile?
[424 byte] By [
homealonea] at [2007-11-27 6:06:18]

It compiles without error because the first argument is a string.
It also runs without any problem because %s is a very accomodating format specifier - it will simply print the string form of the corresponding argument.import java.util.Date;
public class SFormat {
public static void main(String args[]) {
System.out.format("%s%n", new Long("123")); // 123
System.out.format("%s%n", System.out);// java.io.PrintStream@9304b1
System.out.format("%s%n", new Date());// Fri Jun 01 17:46:14 NZST 2007
}
}
From the API: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().