Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Float f = 5;?  RSS feed

 
A Alqtn
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Java 5 introduced the concept of autoboxing which is helpful in some cases. However, if you try to do the following it won't work

Float f = 5;

Does any one know why? In the primitive counterpart example float f = 5; is legally perfect while the autoboxing version is not!!!

Is it a bug or what?

Alqtn
 
Craig Tyler
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, you can Box then Widen, but you can't Widen then Box. Beforehand, if you did float f = 5, the integer would be implicitly widened into a float, but since you can't implicitly widen the int to a float before boxing it as a Float, this doesn't work.
[ February 08, 2006: Message edited by: Craig Tyler ]
 
tayy abdelqader
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can say

float f=5f;

without any problem
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Craig Tyler:
Remember, you can Box then Widen, but you can't Widen then Box. Beforehand, if you did float f = 5, the integer would be implicitly widened into a float, but since you can't implicitly widen the int to a float before boxing it as a Float, this doesn't work.



Integer i='a';

How does this work then ?

Arvind
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arvind Sampath:
Integer i='a';

How does this work then ?


It doesn't. You have to write
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff ,

Yeah, I got an 'incompatibe types' error while compiling from command prompt.

But, surprise surprise, my eclipse IDE let it go scott free. That might sound stupid but i cant help it


I got an output of 97 when i ran the above piece of code.

Will be thankfull to anyone who can throw some light on this



Arvind
 
Lorenz Baylon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got an output of 97 when i ran the above piece of code.

can i make a guess? maybe 97 is the ascii decimal equivalent of the letter 'a' .
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by lhorenz baylon:

can i make a guess? maybe 97 is the ascii decimal equivalent of the letter 'a' .


That's the easy part. The hard past is why it allowed "Integer i = 'a'". Maybe the specs for autoboxing have changed a little or maybe Eclipse has got it wrong...
 
tayy abdelqader
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i do not understand how it could work

I have tried it

it gives acompiler error
 
Lorenz Baylon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it gives a compiler error

have you tried it on eclipse IDE, with java 5 and 'a' as char (i mistakenly tried it as string at first)?
i think im getting interested with this, thanks for bringing up the topic
 
A Alqtn
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Eclipse use different compiler other than Sun's JDK 1.5?

As far as I know Eclipse provides different graphics package (SWT) which is different than Swing.

If Integer i = 'a'; works in one implementation as you are reporting in the case of Eclipse and does not work on Sun's implementation then one of them must have a bug.

Alqtn
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!