the code u've written creates a Byte object by a new operator.....seems as u've not read my question carefully!!!
well let me rephrase......the valueOf() method is a static method for the Wrapper classes and it's overloaded twice. the first one takes appropraite primitive value as a String and returns an Object of a wrapper class with string value inculcated in appropraite form....okie!!!
there is one more valueOf(), which takes two parameters....we're not into that now.....
the question is how do you pass a byte for the Byte.valueOf(???) what do you write for ??? how to write byte value(like 2 is default int, 2.0 is default double, 2f is floating point, etc....)
one way can be : Integer i = new Integer("20"); byte b = i.byteValue();
now we can think of passing this value(which is in b) to the valueOf() as an argument.....but....that dosen't work
Amit, You can check the Long.valueOf() implementation in Long.java file. I hope you know where this file is. In case not, it should be in the path where you downloaded the j2sdk. There is a winzip file src.zip. Just open that zip file and look for Long.java. That will help you to understand the reason.
Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
hi amit, i have some notes.i think it will definitely help u. 1]The Float constructor is overloaded: one version accepts a primitive of type float; one accepts a primitive of type double; one accepts a String representation of a floating-point literal. 2]in constructors of wrapper classes ,arguments of type String can not contain an integer type suffix, L or l. A floating-point suffix, F, f, D or d, is acceptable. suppose, Which of the instance creation expressions produce a run-time
a. new Float('A') b. new Float("A") c. new Float(1L) d. new Float("1L") e. new Float(0x10) f. new Float("0x10") g. new Float("010")
ans:b,d,f explanation: i]The Float constructor is overloaded: one version accepts a primitive of type float; one accepts a primitive of type double; one accepts a String representation of a floating-point literal ii]The String literals "NaN" and "Infinity" are accepted by the Float constructor. A sign (+ or -) is optional. iii]The primitive char literal 'A' is converted to a float, and is accepted by the constructor that declares a parameter of type float. iv]The API specification states that any other String must represent a floating-point value; v]The leading 0 of an octal value is ignored, and the String is parsed as a decimal value. A String representation of a hexadecimal value is not acceptable. vi]The String "A" does not represent a floating-point literal value; therefore, a NumberFormatException is thrown. vii]Arguments of type String can not contain an integer type suffix, L or l. A floating-point suffix, F, f, D or d, is acceptable, but the suffix has no impact on the result. i think this much is sufficient to clear ur first doubt. now i am thinking about second.
How to pass a byte.......Byte b = Byte.valueOf(??);
Look at the code what u r trying to do, u r passing a string "i2.byteValue()" in the argument to Byte.valueOf(), and its quite obvious that it has to throw error.
How to pass a long in ...Long ll = Long.valueOf(3L);//gives Runtime execption(Number Format)
whereas .....Float f = Float.valueOf(3.9f); //works fine.
Thats because the method valueOf() has been implemented differently in the Float class. It can accept f or d as its suffix. So thats how it is. But in the case of Long's valueOf(), its implemented as "new Long(Long.parseLong(s))" So that throws an error.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop