| Author |
Wrapper Doubt
|
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
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
Joined: Nov 20, 2008
Posts: 254
|
|
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);
|
“Everything should be as simple as it is, but not simpler.” Albert Einstein
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
ya Duc Vo is right.
|
SCJP 6
|
 |
Rajshekhar Paul
Ranch Hand
Joined: Oct 17, 2006
Posts: 140
|
|
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 ]
|
When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
|
 |
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12920
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Rajshekhar Paul
Ranch Hand
Joined: Oct 17, 2006
Posts: 140
|
|
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
Joined: Oct 16, 2008
Posts: 952
|
|
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
Joined: Oct 16, 2008
Posts: 952
|
|
|
Float.toString() will take float and primitives smaller than float such as long, int, short, char, byte.
|
 |
 |
|
|
subject: Wrapper Doubt
|
|
|