• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion doubt

 
kaarthick Ramamurthy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • 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
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic