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.
Joined: Sep 16, 2008
But I am getting confused. I dont know how to remember which method takes which parameter?
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:
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.
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 ]
Joined: Oct 16, 2008
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.
Joined: Oct 16, 2008
Float.toString() will take float and primitives smaller than float such as long, int, short, char, byte.