File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in casting

 
Sahul Yasin
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
float f1 = 10L;
Float f = new Float(10L);
Float f = new Float(10.00D);
The above codes are working fine. But in K&B(page 377) says Float constructor accepts only float or String!!?.
I thought, eventhough the long and double values are in the range of float, the compiler will not accept it unless otherwise it is declared as final.
But confused now..
any ideas?..
[ January 29, 2004: Message edited by: Mohamed Yasin ]
 
Xiao Hu
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Mohamed
Here is source code for all 3 of Float constructors
public Float(float value) {
this.value = value;
}
public Float(double value) {
this.value = (float)value;
}
public Float(String s) throws NumberFormatException {
this(valueOf(s).floatValue());
}
1. a long can be implicitly cast to float, compiler would happy to accept that.
2. a double parameter will be simply cast to float, may cause losing precision.
Hope it helps
 
Sahul Yasin
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Xiao Hu.
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohamed,
I've checked K&B, because I was remembering two exceptions
for the constructor arguments for wrappers, namely Character
and Float, which respectively have 1 and 3 possible arguments.
All the others have 2.
In table 6-2 it says the Float constructor can accept float,
double and String.
Greetings,
Gian Franco
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic