Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive casting

 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

float a = 23.23f;
int b = a; //Fails to compile
int i = (int)a; //Compiles
------
int c = 123;
float d = c; //Compiles
------

My questions is, when both float and int are of size 32 bits, why the relation between float and int types is not symmetric?

Thanks,
-Vijay
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if this is difficult question. Please disregard. I will post the same in the Intermediate section.

Thanks,
-Vijay
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is that even though floats and ints are stored in the same number of bits, whole numbers are stored in their binary form whereas floating point numbers are stored as mantissa and exponent. So you can have far larger values in a float than in an int.
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith.

-Vijay
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijay Gade:
Sorry if this is difficult question. Please disregard. I will post the same in the Intermediate section...

This is the appropriate forum for this type of question.

Please do not cross-post. People don't like to answer a post, only to find that it's already been answered in another forum.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though both float and int use the same number of bits, the interpretation of those bits are VERY different. In otherwords, int and float numbers are stored differently. Integer types are stored in twos-compliment format whereas floating point numbers are stored in IEEE 754 format. You should google for these terms to find information on them.

Layne
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
Even though both float and int use the same number of bits, the interpretation of those bits are VERY different. In otherwords, int and float numbers are stored differently. Integer types are stored in twos-compliment format whereas floating point numbers are stored in IEEE 754 format. You should google for these terms to find information on them.

Layne


I am curious why you consistantly answer questions that have previously been answered already.
[ March 02, 2006: Message edited by: Robert Hill ]
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think, there is any harm in the OP gettin more than one perspective on his question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic