codescracker


java

Java Type Casting



« Previous Tutorial Next Tutorial »


Since, not all the types are compatible, and thus, not all the type conversions are implicitly allowed. For instance, there is no automatic conversion defined from double to byte. Fortunately, it is still possible to obtain a conversion between incompatible types. To do so, you must use a cast, which performs an explicit conversion between the incompatible types.

Casting Incompatible Types

Although the automatic type conversions are helpful, they will not fulfil all the needs. For example, what if you want to assign an int value to a byte variable? This conversion will not be performed automatically, because a byte is smaller than an int. This kind of conversion is sometimes called a narrowing conversion, since you are explicitly making the value narrower so that it will fit into the target type.

To create a conversion between the two incompatible types, you must use a cast. A cast is simply an explicit type conversion. It has general form is:

(target-type) value

Here, target-type specifies the desired type to convert the specified value to. For example, the following code fragment casts an int to a byte. If the integer's value is larger than the range of a byte, it will be reduced modulo (the remainder of an integer division by the) byte's range.

int a;
byte b;
// ...
b = (byte) a;

A different type of conversion will occur when a floating-point value is assigned to an integer type: truncation. As you know, integers do not have fractional components. Thus, when a floating-point value is assigned to an integer type, the fractional component is lost.

For example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1. The 0.23 will have been truncated. Of course, if the size of the whole number component is too large to fit into the target integer type, then that value will be reduced modulo the target type's range.

Java Type Casting Example

The following example program demonstrates some type conversion that requires casts:

/* Java Program Example - Java Type Casting
 * This program demonstrates the type casting
 * in Java */

public class JavaProgram
{   
    public static void main(String args[])
    {
        
        byte b;
        int i = 257;
        double d = 323.142;
        
        System.out.println("Conversion of int to byte.");
        b = (byte) i;
        System.out.println("i is " + i + " and b is " + b);
        
        System.out.println("\nConversion of double to int.");
        i = (int) d;
        System.out.println("d is " + d + " and i is " + i);
        
        System.out.println("\nConversion of double to byte.");
        b = (byte) d;
        System.out.println("d is " + d + " and b is " + b);
        
    }
}

When the above Java program is compile and run, it will produce the following output:

java type casting

Here, when the value 257 is cast into a byte variable, the result is the remainder of the division of 257 by 256 (the range of a byte), which is 1 in this case. When d is converted to an int, its fractional component is lost. When d is converted to a byte, its fractional component is lost, and the value is reduced modulo 256, which is 67 in this case.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test