File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt About implicit  typecasting 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 "Doubt About implicit  typecasting" Watch "Doubt About implicit  typecasting" New topic

Doubt About implicit typecasting

Ram Mohan Khambhampati

Joined: Sep 04, 2006
Posts: 6
why does java allow implicit casting of int variables like:

byte b=1

and does not do the same in case of float like

float f=1.2

Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
I believe it is to do with precion and the way a value is constructed. I.e. the population of a byte just happens to be the same as that for a short and an int.

By looking at the table below you will see the format of each of the primative datatypes.

Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object) null
boolean false

so you will have to populate the variable instance as follows:
float f=1.2f; or float f=1.2F;

If at first you don't succeed, try, try again. If you don't try you have failed.
Boini Naveen

Joined: Feb 05, 2007
Posts: 2
In java impicit casiting is done only with byte,char,short primitive data types only and default value of intiger primitive is int and for the above primitive datatypes int is implicitly type casted,but for the floating point primitive types double is the default type and when you are trying to assing the floating value the variable like
here 2.5 is nothing but the double value which cannot implicitly type casted to flaot so we need to explicitly mention as
sandrine chen

Joined: Jan 05, 2007
Posts: 7
ehn,... in case you are still confused, I suggest you go and study the K&B book for SCJP 1.4, there is very good explanation of how byte, char, short, int, long, float, double are constructed, then you will understand how "implicit" casting during assignement is permitted.

Basically, it depends on number of bits every data type is holding. In your example, byte is 8-bit, data range is -127 ~ 128, so 1 (literally a int value) is within the range, so it can be assigned to byte type.

Your float question can be answered in the book too, 1.0 (literrally a double value) should be a double type, so if you want to assign it to a float type, you should add "f" or "F" at the end of the value. --> 1.0f

If you want to be more amused, int 1 can be assigned to byte variable only like what you've written, if you try this:
int i = 1;
byte b = i; //compile error

In this case you should cast explicitly. byte b = (int) i;

marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Quintin Stephenson:
I believe it is to do with precion and the way a value is constructed...

Yes, "precision" is the key.

Integral values (byte, short, etc.) are exact binary representations. They can be widened -- or even narrowed, if within range -- without losing any information. But floating-point values (float and double) are often binary approximations with different levels of precision.

Basically, this is because floats and doubles have a limited number of bits to store fractional values, and not all fractional values can be expressed in binary (sums of negative powers of 2) using the bits available.

See Some things you should know about floating-point arithmetic.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Doubt About implicit typecasting
It's not a secret anymore!