Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Conversion doubt

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code, byte b = 100 works fine. 100 is an integer literal and it is assigned to byte variable b. Compiler automatically cast integer to byte in this case as 100 is within the range.

But float f = 100.25 produces compiler error. Why compiler doesn't cast this automatically?

Either we have to type float f =100.25f or float f=(flost)100.25.

Please explain me why it is required while handling float?



Thanks in advance.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kaarthick

It is because all the floating point values are implicitly double in Java.
Hence you are trying to assign a double value say,(1.58) to float.

Hence explicit cast is required.

note that float f=1 doesnt give an error
while float f=1.1 give an error,as 1.1 is a double and explicit cast is required

Thanks
Praveen SP
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default 100.25 is taken as a double. To assign 100.25 to a float we have to add an extra f or cast it to a float.

Read this;

The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).

Hope i'm able to make some sense,

Gitesh
 
kaarthick Ramamurthy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still I am not clear.

My question is,

byte b = 100 works fine.

* 100 is an integer literal. It fits within byte range.
* So compiler automatically cast here.

float f = 100.25;

* 100.25 is a double literal. It fits within float range.
* So Why compiler doesn't cast automatically?

I hope my question make sense.

Thanks
Kaarthick
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, in case of integers, range has to do with the maximum and minimum allowed values. And when within range, no explicit casting is required.
Unlike this, in float and double have more to do with "precision" i.e. how many places afterr decimal can it represent. See below an extract from the Java Spec:

The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).



You might want to look here.
Hence I think we cannot conceptualy equate the case of an "in-range" integer assignment to a byte and a double assignment to a float.
[ August 23, 2007: Message edited by: Satya Maheshwari ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's really no logical explanation for it. It is simply because the designers of the Java decided to make a special case for byte, but not for float.

Note that with float, you can write a float literal value in your code by appending 'F' or 'f' after the number:

float value = 100.25f;

But for byte, there is no special way to specify a byte literal. The only thing you can do is use a cast:

byte value = (byte) 100;

The designers of the Java language probably thought this was too cumbersome, so they invented the special case for byte, so that the cast is not necessary.
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic