Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between valueOf() and parseXXX()

 
Saumyajit Goswami
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 1328
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Himai thanks a lot.now I can understand it...
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic