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 help with data types and casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with data types and casting" Watch "help with data types and casting" New topic
Author

help with data types and casting

Momen Travolta
Greenhorn

Joined: Mar 19, 2010
Posts: 8
I can`t deduce the rules! the results are mostly unexpected to me! please help me.



About casting:-
Do I have to use casting only when assigning a higher data type to a lower one?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Momen Travolta wrote:
About casting:-
Do I have to use casting only when assigning a higher data type to a lower one?


Casting must be explicit if the source range is not within the target range. This means that an double must be explicited casted to a long, but not vica versa. And in the case of a short and char, it means that casting must be done in both directions.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Momen Travolta wrote:

Long.MAX_VALUE is 2^63-1, or 9223372036854775807. Both a and b are larger than this number. c is shorter but the number is an int, and is larger than Integer.MAX_VALUE (2^31-1 or 2147483647). Adding the L tells the compiler that this literal is not an int but a long.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Line 1: You are dividing a float with an int. The result will be a float. Floats are limited to about 6 or 7 digits of precision.
Line 2: You are dividing a long by an int. The result will be a long. Because it is an integer division, the result is 0.
Line 3: Similar to line 2, integer division, result is 0.
Line 4: Dividing a double by an int, the result is a double, which has more digits than a float (line 1).

Line 7: Floating-point literals are double by default, you can't assign them to a float without casting or appending an "f".
Lines 8-9: Assigning values to float, what are you unclear about here?
Line 10: Assigning an int literal to a float is allowed, you might loose some digits.

Line 14: The int literal is too big to fit into a 32-bit int, so you get an error.
Line 15: The long literal is too bit to fit into a 64-bit long, so you get an error.
Line 16: The int literal is too big to fit into a 32-bit int, so you get an error.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
What's a higher data type? What's lower data type? I don't think those are standard Java terms.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with data types and casting