permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between valueOf() and parseXXX() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between valueOf() and parseXXX()" Watch "difference between valueOf() and parseXXX()" New topic

difference between valueOf() and parseXXX()

Saumyajit Goswami

Joined: Oct 04, 2012
Posts: 2
what is the difference between
Integer i5=Integer.parseInt("1010100",2); and int i5=Integer.parseInt("1010100",2);

int i4=Integer.valueOf("101011",2); and Integer i4=Integer.valueOf("101011",2);
if parseXxx() returns the primitive type and valueOf() returns a wrapper object reference of the type then why this is compiling without any error?

public class WrapDemo{
public static void main(String[] args){

static void takeVal(Double d){

System.out.println("It is an object");

if parseDouble returns only a primitive value then why takeVal() method takes the argument of type primitive instead of a Double object?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 1124
Integer.parseInt() returns an int.

With autoboxing,

The integer is returned and boxed as the Integer object.

Double.parseDouble("4'") returns a double. But when it is passed to takVal(Double d), the double is auto boxed.

For more details about auto boxing, refers to KB's study guide.
Saumyajit Goswami

Joined: Oct 04, 2012
Posts: 2
@Himai thanks a I can understand it...
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Im addition to Himani's post-
auto-boxing introduced in java 5.
So, if you want a compile time error then compile it with prior to java 5.

OCPJP 6 86%
I agree. Here's the link:
subject: difference between valueOf() and parseXXX()
It's not a secret anymore!