• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Float wrapper from double. Bonkers?  RSS feed

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

One can't assign a double to a float primitive without a cast...as we know.

So...can anyone explain the reasoning that permits a Float Wrapper to be created by passing a double to the Float constructor?

package Chapter6;

public class wrapperTest {

public static void main(String args[]) {
float floatMax = Float.MAX_VALUE;
double doubleMax = Double.MAX_VALUE;

System.out.println("floatMax=" + floatMax);
System.out.println("doubleMax=" + doubleMax);

//Double from double
Double wrapperDouble = new Double(doubleMax);
double doubleOut = wrapperDouble.doubleValue();
System.out.println("doubleOut (From wrapperDouble holding doubleMax)=" + doubleOut);

//Float from float
Float wrapperFloat = new Float(floatMax);
float floatOut = wrapperFloat.floatValue();
System.out.println("floatOut (From wrapperFloat holding floatMax)="+ floatOut);

//Create a Float Wrapper using Float(double x) constructor (silly)
Float wrapperFloat2 = new Float(doubleMax);
float floatOut2 = wrapperFloat2.floatValue();
System.out.println("floatOut2 as float (From wrapperFloat2 holding doubleMax)=" + floatOut2);
double doubleOut2 = wrapperFloat2.doubleValue();
System.out.println("doubleOut2 as double (From wrapperFloat2 holding doubleMax)=" + doubleOut2);
}

}

Output...

floatMax=3.4028235E38
doubleMax=1.7976931348623157E308
doubleOut (From wrapperDouble holding doubleMax)=1.7976931348623157E308
floatOut (From wrapperFloat holding floatMax)=3.4028235E38
floatOut2 as float (From wrapperFloat2 holding doubleMax)=Infinity
doubleOut2 as double (From wrapperFloat2 holding doubleMax)=Infinity


Cheers,

Si.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Float constructor is overloaded. One version accepts a primitive of type float; one accepts a primitive of type double; one accepts a String representation of a floating-point literal.
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kedar,

I understand the Float constructor is overloaded. I have no issue with the versions that take a String or a float.

Moreover, I understand that it CAN take a double. My question is WHY was it done that way?

If java won't let one assign a double value to a float primitive(without a cast); then why allow a Float Wrapper to be created from double primitive (without a cast)?

Cheers,

Si.
[ February 27, 2005: Message edited by: Simon Cockayne ]
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Simon]: If java won't let one assign a double value to a float primitive; then why allow a Float Wrapper to be created from double primitive?

You can assign a double to a float - if you explicitly cast it to float. That means that, if you go to the extra effort to tell the Java compiler to go ahead and convert the double to a float, it will. It won't do this by default, but will if you explicitly tell it to. Similarly, you can explicitly specify that you want to convert a double to a Float, by explictly calling the constructor. The rules are not written to prevent you from ever converting a more precise data type to a less precise data type. They were jst written to make this a little bit harder, forcing you to explicitly acknowledge the conversion in some way. Calling the new Float(double) constructor is much like casting a double to a float - you're allowed to do it, as long as you tell the compiler that's really what you want.
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morning Jim,

Thanks for the reply. I understand what your saying, but I still disagree, see below.

package Chapter6;

public class testWrapper2 {

public static void main(String[] args) {
float myNumberA = 25;
double myNumberB = 100;

//Primitive assignment:
float myFloat = myNumberA;
//float myFloat = myNumberB; //Cannot convert from double to float without cast
float myFloat2 = (float)myNumberB; //Can convert with cast

//Wrapper assignment:
Float myFloatWrapper1 = new Float(myNumberA);//Float from float

//In the next line I CAN create a Float from a double without explicitly
//saying, yes I know it is a double, but "trust me"...
Float myFloatWrapper2 = new Float(myNumberB);//Float from double

//I would have thought that this should have been the only way to allow
//a Float to be created from a double...
Float myFloatWrapper3 = new Float((float)myNumberB);//Float from (float)double
}
}

See what I mean?

Cheers,

Si.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no real logic in overloading the Float construtor with a double parameter (and not doing it for the Short parameter with int).

Now we have the following:

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I guess you're (both) right - it doesn't really make any sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!