| Author |
difference between valueOf() and parseXXX()
|
Saumyajit Goswami
Greenhorn
Joined: Oct 04, 2012
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
Joined: Jul 29, 2012
Posts: 287
|
|
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
Joined: Oct 04, 2012
Posts: 2
|
|
|
@Himai thanks a lot.now I can understand it...
|
 |
Nikhil Sagar
Ranch Hand
Joined: Apr 21, 2012
Posts: 214
|
|
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%
|
 |
 |
|
|
subject: difference between valueOf() and parseXXX()
|
|
|