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

Wrapper Doubt

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int q=1;
Integer i2=Integer.valueOf("q");// Runtime Error.
wheras,
String s= Integer.toString(q); // Compiles and runs fine.

Why is it not taking the value q=1 in case of valueOf() method?
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhi vijay:
int q=1;
Integer i2=Integer.valueOf("q");// Runtime Error.

Because instead of passing q as a variable, you've passed string "q" in, hence the error.
It should be:

Integer i2=Integer.valueOf(q);
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya Duc Vo is right.
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi, a little look at the API documentation for the wrapper classes will help you understand what parameters can be passed to which wrapper class method.
[ December 09, 2008: Message edited by: Rajshekhar Paul ]
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I am getting confused. I dont know how to remember which method takes which parameter?

Ex:
Float f2=Float.valueOf("1.0");// Fine.

But,
String f=Float.toString(1.0);// Error.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your last example, you get an error because a floating-point literal like 1.0 is of type double (not float). The method Float.toString(...) takes a float, and a double is not converted automatically to a float.

You can make a floating-point literal a float by appending an f or F behind it, for example:

String f=Float.toString(1.0f);

If something is in "quotes", then it is a string in Java, and not an expression that is interpreted like any other expression. So "1.0" is a string containing three characters, it's not a number.
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are getting error because you have passed a double primitive inspite of a float.

The above code will work fine.
Now, the Float.toString() always takes a float only. And in Java to mention a float primitive, you have add 'f' or 'F' after the value like 1.0f. Otherwise it will be treated as a double by default.
[ December 09, 2008: Message edited by: Rajshekhar Paul ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually valueOf(primitive/String); is used to convert String/primitive to wrapper class.
So Double.valueOf(double) can take any primitive. char, byte, short, int, long, float, double. as all primitives will be uppercasted.
and can take String representing primitive value.

same way Float.valueOf(float); can take primitives char, byte, short, int, long but cannot take double. double needs explicit cast here.

toString(); used to convert primitive to string, so it will only take primitive arguments.

If you concentrate on the purpose of methods, it will be clear to you.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Float.toString() will take float and primitives smaller than float such as long, int, short, char, byte.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic