Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

format() question

 
Tamara Lopez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As part of Q9 Chapter 6(p503) in K&B:
Will this compile:
System.out.format("%s",new Long("123"));

The answer is yes.

But new Long would return a Long - which is not a String. But it does work. Why??
Thanks
 
Oscar Gonzalez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Long.toString() method is called to make the cast. The compiler says: "I know you want to convert the Long to a String and do it automaticaly"

 
Changchun Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because it implicitly invoke the new object`s toString{}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic