• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper Class test : Long Class

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Could anyone explain why NumberFormatException is thrown in the
code below?
CODE
------
<pre>

public class WrapperTest
{
public static void main(String a[])
{
Long l1 = new Long("10L"); // NumberFormatException.
Long l2 = new Long("10l"); // NumberFormatException.
Float f = new Float("10.3f"); // OK.
}
}

</pre>
------------------
Regards
---------
vadiraj

*****************
There's a lot of I in J.
*****************
 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fine.As in wrapper class of long i.e., Long(String s) definitely
takes string as a parameter which is parsed into long with valueOf() which returns a new long object initialized to the value of the specified String. Throws an exception if the String cannot be parsed as a long. The radix is assumed to be 10.
so it cant take any extension character like "l" or "L".
where in Float, doesnt consider radix in valueOf() while parsing.
so u can try out in the following way.

public class WrapperTest{
public static void main(String a[]){
long L1=10L;
String L2="10";
Long l1 = new Long(L1);
Long l2 = new Long(L2);
Float f = new Float("10.3f");
}
}
Hope it helps..
Anil.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrapper class Long(string s) wraps a primitive long value represented by the string in decimal form. The String s is converted to the long value by calling the method ParseLong(String, radix) with radix 10 so that it converts the string to signed long with base 10.
ParseLong throws a number format exception if any character of the string after the first is not a digit of the specified radix.
wrapper class Long in turn throws a NumberFormatException since the string is not a parsable long integer. This is why you are getting two NumberFormatException 's while you execute the code.
Kalidas.
 
reply
    Bookmark Topic Watch Topic
  • New Topic