aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrappere question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrappere question" Watch "Wrappere question" New topic
Author

Wrappere question

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
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.


The Inner that is named is not the true Inner.
Vicky Jain
Ranch Hand

Joined: Aug 02, 2003
Posts: 52
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 );
}
}


SCJP1.4(96%)<br />A drop of ink can make millions think.
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
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

Joined: Jun 20, 2002
Posts: 442
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
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jun 20, 2002
Posts: 442
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

Joined: Aug 02, 2003
Posts: 52
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

Joined: Sep 03, 2003
Posts: 125
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

Joined: Jun 20, 2002
Posts: 442
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

Joined: Aug 05, 2002
Posts: 787
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Wrappere question