• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ValueOf()

 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("54321".valueOf(12345)) ;
Result : 12345
Why does it give result as given above ?
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because valueOf() is a static method. so your call is no different than

your "54321" just help establish that the method to be called belong to
the String class as opposed to Integer.valueOf(), Byte.valueOf(), etc.
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"54321" is an instance of String. valueOf() is a static method of String. It's perfectly legit to access a static method via an instance, and in this case the contents of the instance make no difference. String.valueOf(12345) and "".valueOf(12345) will also produce the string 12345.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic