This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# difference between valueOf() and parseXXX()

Saumyajit Goswami
Greenhorn
Posts: 2
what is the difference between
Integer i5=Integer.parseInt("1010100",2); and int i5=Integer.parseInt("1010100",2);

also
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){

takeVal(Double.valueOf("4"));
takeVal(Double.parseDouble("4"));
}
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
Posts: 1342
7
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
Greenhorn
Posts: 2
• 1
@Himai thanks a lot.now I can understand it...

Nikhil Sagar
Ranch Hand
Posts: 216