wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to pass a long in ...Long ll = Long.valueOf(3L);//gives Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to pass a long in ...Long ll = Long.valueOf(3L);//gives " Watch "How to pass a long in ...Long ll = Long.valueOf(3L);//gives " New topic
Author

How to pass a long in ...Long ll = Long.valueOf(3L);//gives

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi all,

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.


one more question:

How to pass a byte.......Byte b = Byte.valueOf(??);

i tried:


but i get a Number Format Exception!!!

help!!!

amit
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Amit,

You need to check out the constructor and conversion methods available to "wrapper" classes.




Cheers,

Si.


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hey Simon,

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

similar is my next question out there....

thanx
amit
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
Simon's
Float F = Float.valueOf("4.5")


Simon .....4.5 is a double number okie!!!

dont you think there shud be an error.....

secondly how do you pass long for Long.valueOf(3L)//gives NumberFormat Exception
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
sorry its Long.valueOf("3L") which gives error.....plz make a note....
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Amit,

I was just trying to show you some different examples.

for *wrapper*.valueOf(String)

Byte B2 = Byte.valueOf("5");

System.out.println(B2);

Cheers,

Si.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
now u c ......as in your example 5 is actually an int value which you're passing...correct me if i'm wrong....my question is how do you pass a byte there....?
hope mow you'll get my point..
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Amit,

It is not an int or a byte...it is a String.

Byte.valueOf takes a String and turns it into a wrapper class, i.e. Byte.

Cheers,

Si.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
u're right that the valuOf() method takes a String but that String is actually aupposed to be of appropriate kind of primitive value:
for eg:

Float f = Float.valueOf("3.4f"); //works
Float f1 = Float.valueOf("3.4"); //works

so when we write 3.4f and if we write 3.4 dont you think its diff???

why doesn't
Long l = Long.valueOf("3L") work??? do u have an answer for this...??
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
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.
prajkta patil
Ranch Hand

Joined: Nov 13, 2004
Posts: 49
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

error?

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.
prajkta patil
Ranch Hand

Joined: Nov 13, 2004
Posts: 49
same rules r applied for valuOf method only difference is that value of method takes String or wrapper class objects not primitives.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
thanx Prajakta,

ur explanation is helpful....
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

How to pass a byte.......Byte b = Byte.valueOf(??);

i tried:



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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass a long in ...Long ll = Long.valueOf(3L);//gives