Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper class

 
C Law
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Float f = 13.0; // compiler error

Float f = new Float(13.0); //OK

Can anybody tell me why this is so?

Thanks.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by C Law:
Float f = 13.0; // compiler error

Float f = new Float(13.0); //OK

Can anybody tell me why this is so?

Thanks.


Float is wrapper class for float primitive data type. When you create new object you need to use new keyword except for String.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And note that if you are using JDK 1.5, the assignment of a primitive to an object (or vice versa) is perfectly valid. The code

won't generate any compilation error. This feature is called as Auto-boxing.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mani Ram:
And note that if you are using JDK 1.5, the assignment of a primitive to an object (or vice versa) is perfectly valid. The code

won't generate any compilation error. This feature is called as Auto-boxing.


Thanks Mani Ram, I forgot to mention this.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recall that floating-point literals (like 13.0) are interpreted as type double.

Autoboxing will convert a float to a Float or a double to a Double, but not a double to a Float. See JLS: 5.1.7. This is why Float f = 13.0; fails.

However, there is a Float constructor that takes a double as an argument. This is why Float f = new Float(13.0); is okay.
 
C Law
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I am using JDK 1.5.

The point is actually not autoboxing.
On my machine,

Float f = 13.0; // compiler error
Float f = 13.0f; //Autoboxing works.

I just can't see why I have to explicity designate a float
to be autoboxed, especially Float's constructor accepts
double.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mani Ram:
And note that if you are using JDK 1.5, the assignment of a primitive to an object (or vice versa) is perfectly valid...

Not in all cases.

Double d = 13.0; //OK
Float f = 13.0f; //OK

But...

Float f2 = 13.0; //error!

incompatible types
found : double
required: java.lang.Float
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops...you are right. Just forgot to remember the basics
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic