File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Primitive casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Primitive casting" Watch "Primitive casting" New topic
Author

Primitive casting

Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
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

Joined: Jul 04, 2005
Posts: 81
Sorry if this is difficult question. Please disregard. I will post the same in the Intermediate section.

Thanks,
-Vijay
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jul 04, 2005
Posts: 81
Thanks Keith.

-Vijay
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"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
sscce.org
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
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

Joined: Feb 14, 2006
Posts: 210
I dont think, there is any harm in the OP gettin more than one perspective on his question.


SCJP 1.4 85%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Primitive casting
 
Similar Threads
question on wrapper class
Overloading doubt
array
Overload
Primitive casting