Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt About implicit typecasting

 
Ram Mohan Khambhampati
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Boini Naveen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
float=2.5
here 2.5 is nothing but the double value which cannot implicitly type casted to flaot so we need to explicitly mention as
float=2.5f
 
sandrine chen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

regards,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic