Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

valueOf() vs parseXXX()

 
Chaitanya Jadhav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to K & B
The difference between the two methods is

* parseXxx() returns the named primitive.
* valueOf() returns a newly created wrapped object of the type that invoked
the method.

But both method return primitive as well as Object.??
then what is difference between them?



 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaitanya the book is right. valueOf returns a wrapper class object. This code



works only because of autoboxing. valueOf here returns an Integer object which is unboxed to int. To test this, try this code



Same is the case with parseInt here

 
Chaitanya Jadhav
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit...

nice example .
thanks..
now i understood..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic