• 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

Wrappere question

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class wrapper1{
public static void main(String args[]){
Float f=Float.valueOf("1.2");
}
}
The above code compiles & runs fine.Shouldn't it throw runtime exception?Coz we are giving double as argument to valueof method which cant be implicitly converted to float?
Veena
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What we are giving to the method is a FloatingPointLiteral. (If you check the javadocs description of Float.valueOf() there is a link to the JLS definition of FloatingPointLiteral.) The gist of it is: you can append the "F" for Float but don't have to. In any case the value will be parsed and the compiler will complain only if the result is too big for a Float.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
i would alsolike to know that why this too compiles fine & run.
class wrapper1{
public static void main(String args[]){
Float f=Float.valueOf( 1.2d );
}
}
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class wrapper1{
public static void main(String args[]){
Float f=Float.valueOf( 1.2d );
}
I compiled this code. but it is giving error. since valuOf takes String as argument. if u make as follows
Float f=Float.valueOf( "1.2d");
This will compile and run fine.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Lovelace:
What we are giving to the method is a FloatingPointLiteral. (If you check the javadocs description of Float.valueOf() there is a link to the JLS definition of FloatingPointLiteral.) The gist of it is: you can append the "F" for Float but don't have to. In any case the value will be parsed and the compiler will complain only if the result is too big for a Float.



You mean float f=2.0; is fine?I don't think so ....... It gives compiler error of possible loss of precision....
Veena
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. I do not think that string argument is parsed at compile time. Therefore, compiler will not give any error even if you put "aaa" as argument.
2. Any numerical argument that can be promoted to float, will be promoted.
3. If string contains a number beyond the limits of a float, + or - infinity will be printed.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barkat Mardhani:

2. Any numerical argument that can be promoted to float, will be promoted.


But in the line Float f=Float.valueOf("1.2"); ,the value 1.2 can't be promoted to float....
 
Vicky Jain
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class wrapper1{
public static void main(String args[]){
Float f=new Float(5.6d);
}
}
can anyone explain me that why this code compiles fine & run even the Float wrapper class doesn't provide any constructor with double as argument, Float wrapper class contains only two constructors, one with float & other with string as argument.
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barkat,
Thanks for pointing this out. The compiler is happy with ANY string you give the constructor. At runtime, if the jvm can't make sense of it, you get a NumberFormatException. As you say, if it's too big, you get +- Infinity. But as Veena points out, you don't mean "Any numerical argument that can be promoted to float". Any argument that the jvm can make into a floating point number will do.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vicky Jain:
class wrapper1{
public static void main(String args[]){
Float f=new Float(5.6d);
}
}
can anyone explain me that why this code compiles fine & run even the Float wrapper class doesn't provide any constructor with double as argument, Float wrapper class contains only two constructors, one with float & other with string as argument.


Vicky,
Check out Java API ,Float has 3 overloaded constructors.
1.which takes float as an argument
2.which takes double argument
3.which takes string argument
Steve thanks .Knowing that Float can take String argument that is double is very imporatant.Just to verify , it is same with Byte,Short.I mean Byte ,Short constructors can take String argument that is integer.
Thanks
Veena
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
Let me eloborate on what I posted before:
1. I do not think that string argument is parsed at compile time. Therefore, compiler will not give any error even if you put "aaa" as argument.
2. Any numerical argument that can be promoted to float, will be promoted.
e.g. int argument will be promoted to float:
Float f = Float.valueOf("1");

3. If string contains a number beyond the limits of a float, + or - infinity will be printed.
What I meant was that "If string contains a floating point number (float or double) that is beyond the limits of a float, + or - infinity will be printed."

Hope it makes sense now.
Barkat
 
reply
    Bookmark Topic Watch Topic
  • New Topic