Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversion doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversion doubt" Watch "Conversion doubt" New topic
Author

Conversion doubt

kaarthick Ramamurthy
Greenhorn

Joined: Aug 17, 2007
Posts: 12
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

Joined: Jul 17, 2007
Posts: 95
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

Joined: Feb 28, 2007
Posts: 274
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

Joined: Aug 17, 2007
Posts: 12
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

Joined: Jan 01, 2007
Posts: 368
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 ]

Thanks and Regards
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
jQuery in Action, 2nd edition
 
subject: Conversion doubt