• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Float f = 5;?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u can say

float f=5f;

without any problem
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic