• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,

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




Cheers,

Si.
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry its Long.valueOf("3L") which gives error.....plz make a note....
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...??
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx Prajakta,

ur explanation is helpful....
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
reply
    Bookmark Topic Watch Topic
  • New Topic