• 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 doubt

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do these wrapper class declarations give run time exception.

Float a=new Float("A");
Float b=new Float("1L");
Float c=new Float("0x10") ;

please explain me..

Thank you,
sunitha
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

1) Float a=new Float("A");

The Float instance will convert the string "A" into float value at runtime. Since, "A" is not a valid number, runtime exception arises.

2) Float b=new Float("1L");

Again, "1L" is long value. Float is not capable of storing a long.

3) Float c=new Float("0x10") ;

Same reason as above.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mani,

Got the point for the 1 and 2. But in the 3, isn't it a int type literal??
In that case it should work.

Regards,
Mausam
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai all,

my opinion is whatever u give inside the double-quotes of the floating-point constructor,it must be leagal floating-point literal.And it must be parsable to a valid decimal integer.


The API specification states that String must represent a floating-point value; however, a little experimentation proves that a String is acceptable if it can be parsed as a decimal integer .

what do u say Mausam....
 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashok,

however, a little experimentation proves that a String is acceptable if it can be parsed as a decimal integer .



Got that.

Regards,
Mausam
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this code and it gave run-time error, too:

Long ll=new Long("1L");

It seems "1L" can't be passed as the string parameter to all primitive wrapper constructors.
 
sunitha thejaswi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for response..
but the if new Float("A") is not valid then how come new Float('A'); is valid..how is 'A' here converted to float...
please explain...
could anyone suggest me a good study material for wrapper classes...
thank you
sunitha
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
the following works fine

class Example
{
public static void main(String args[])
{
long l=50;
Float b=new Float("1");
Float d=new Float(l+"");
}
}


I assume the string parameter to the Float constructor has to be a valid number and not something like 1D or 1D or 1F. Even 1f doesnt work for me..

Correct me if I am wrong
 
Mani vannan
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Float('A') --> 'A' means the ascii value of the char 'A', which is again an integer. Integer can be assigned to a float without a cast and it's really legal to do so.

Also Musam,

when parsing the hexa value to a float, it might have caused the runtime exception.
 
sunitha thejaswi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manivannan
If 'A' is converted into int value like you said my doubt is y not "A" is not converted...
the whole thing is sooo confusing...
thank you
sunitha
 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunitha,

A quick question...what do you think when you see...

String str = "some string" in your code... or say
just "some string" in your code.

Regards,
Mausam
 
sunitha thejaswi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a string object with that some string stored in it right..if there is more to it please explain...
thank you..
sunitha
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunitha,
the basic thing is --'A' is Integral type, where as "A" is not.
 
Wendy Lum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cheenu Subramanian:
I assume the string parameter to the Float constructor has to be a valid number and not something like 1D or 1D or 1F. Even 1f doesnt work for me..Correct me if I am wrong



Float ff=new Float("1f");
Float ff1=new Float(1f);

Work fine here.
 
Cheenu Subramanian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya Wendy.. U r right..

Float f=new Float("0x1") doesnt work.. Any idea ..why?
 
Wendy Lum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cheenu Subramanian:
Ya Wendy.. U r right..

Float f=new Float("0x1") doesnt work.. Any idea ..why?


I don't know why. Just try to memorize it.
[ November 24, 2005: Message edited by: Wendy Lum ]
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Cheenu Subramanian:
Ya Wendy.. U r right..

Float f=new Float("0x1") doesnt work.. Any idea ..why?



i believe float literals won't work for hexadecimals and octals.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
 
Cheenu Subramanian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Float f=new Float("011");

f when printed gives 11.0 instead of 8
(if it were considered as octal.)

So may be Octal and Hexadecimal wont work with String literals passed to Wrapper constructors
bcoz even this
Integer f=new Integer("0x11");
gives a Number Format exception..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic