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 type casting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "type casting" Watch "type casting" New topic
Author

type casting

Mehul Mistry
Ranch Hand

Joined: Feb 10, 2006
Posts: 43
hi...
I want to that why in java we can't do type casting from higher datatype to lower dataype.I mean
float f=2.45f;
int i=(float)f;

What exactly happens here at at memory level.Is it because of the two different way bot the data type are represented internally.If yes.. can someone pls explain it.

Thanks.
Jimmy Hansen
Greenhorn

Joined: Aug 22, 2006
Posts: 4
Please note that you are not doing a cast from a float to an int, you are just trying to assign a float to an int.

The correct option would be:
int i=(int)f;

And then i=2.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: type casting
 
Similar Threads
converting float to double
primitive type casting problem.........
Float a=3 doesn't compile but Short a=3 does. Why ?
why ceil & floor accepts int
Casting & Conversion !