Difference between revisions of "Casting"
Line 1: | Line 1: | ||
== Introduction to Casting == | == Introduction to Casting == | ||
− | When working with various primative types in Java, it may be necessary to convert between these types. Casting | + | When working with various primative types in Java, it may be necessary to convert between these types. Casting occurs within an expression, and has the effect of converting from one type of data to another. So, to cast to a particular type, is to convert an existing value to that type. What this conversion entails depends on the type of data being cast. For instance, casting a primitive floating point value (type <code>float</code>) to an integer value (type <code>int</code>) means that the decimal portion of the <code>float</code> will be lost, because it cannot be represented by an <code>int</code>. As a general rule, casting will always try to preserve as much of the data as possible. |
− | + | ||
== Types of Casting == | == Types of Casting == | ||
+ | |||
+ | There are two different ways to Cast in Java. One can cast implicitly or explicitly. | ||
=== Explicit === | === Explicit === | ||
+ | |||
+ | An explicit cast occurs when the programmer writes out the type to be cast to as part of an expression. Explicit casts are easy to notice, because they use the following form: | ||
+ | |||
+ | <code> | ||
+ | float floatingPoint = 30.5; | ||
+ | int integerValue = (int)floatingPoint; | ||
+ | </code> | ||
+ | |||
+ | As you can see, the type being cast to occurs in brackets before the data that is being cast. In the above example, the <code>float</code> ''floatingPoint'' is cast to type <code>int</code>, and is assigned to the variable ''integerValue''. As mentioned, this has the effect of assigning <code>30</code> to ''integerValue'', because <code>int</code> variables cannot represent the decimal <code>.5</code>. | ||
=== Implicit === | === Implicit === | ||
+ | |||
+ | An implicit cast occurs when a value must be converted to a different type for some reason. Such reasons may include: | ||
+ | * Assignment of a variable of one type to a variable of another, compatible type | ||
+ | * Conversion required as part of an expression | ||
+ | |||
+ | |||
+ | |||
+ | [[Category:Java]] | ||
+ | [[Category:COMP_1010]] |
Revision as of 19:11, 23 February 2007
Introduction to Casting
When working with various primative types in Java, it may be necessary to convert between these types. Casting occurs within an expression, and has the effect of converting from one type of data to another. So, to cast to a particular type, is to convert an existing value to that type. What this conversion entails depends on the type of data being cast. For instance, casting a primitive floating point value (type float
) to an integer value (type int
) means that the decimal portion of the float
will be lost, because it cannot be represented by an int
. As a general rule, casting will always try to preserve as much of the data as possible.
Types of Casting
There are two different ways to Cast in Java. One can cast implicitly or explicitly.
Explicit
An explicit cast occurs when the programmer writes out the type to be cast to as part of an expression. Explicit casts are easy to notice, because they use the following form:
float floatingPoint = 30.5; int integerValue = (int)floatingPoint;
As you can see, the type being cast to occurs in brackets before the data that is being cast. In the above example, the float
floatingPoint is cast to type int
, and is assigned to the variable integerValue. As mentioned, this has the effect of assigning 30
to integerValue, because int
variables cannot represent the decimal .5
.
Implicit
An implicit cast occurs when a value must be converted to a different type for some reason. Such reasons may include:
- Assignment of a variable of one type to a variable of another, compatible type
- Conversion required as part of an expression