• 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

Problem in casting

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Xiao Hu.
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic