Witscale Test Center

10.3 Type conversion in primitives > 10.3.6 Primitive type casting


10.3.6 Primitive type casting

The type conversions in Java are an automatic type change. Java performs them wherever type is to be changed from narrower type to a wider type.  Since the type conversions are implicit, you need not tell Java when to perform them. Java senses the need and performs them automatically. Casting on the other hand is a different story. When you cast a type into another type, you are explicitly telling Java to make the type change. The cast operator is used to specify the type. Casting is necessary when you wish to change a wider type into narrower type, the code does not compile as it is. For example, the following code does not compile as wider data type  short in being store in to a narrower data type byte:

 

short s = 255;

byte b = s;  // Compile-time error!

 

 Usually with this kind of type change, there is risk of loosing some information. Since byte can store only -128 to +127 numbers, it will most certainly loose information while storing short value 300. The cast operator is a way of telling compiler that you are taking that risk consciously. For example, following code will compile with a cast operator:

 

short s = 255;      // Equivalent hexadecimal value ==> 0xFF

byte b = (byte) s;  // casting of s, value of b will be -1

 

Though this code compiles, it is trying to store a wider (bigger) value in a narrower (smaller) container. In order to fit, the value must be shrunk into a byte size. Figure 10.4 shows the shrinking of short value 255 to fit in into a byte variable during casting.

 


Figure 10.4 Casting of 16 bit short value to 8 bit byte value

 

The byte has only 8 bits. Therefore, the 8 bits on the left side of short value are simply stripped off to fit in into a byte variable. This causes the magnitude of the value to change. In this particular example, the sign is also changed. Value of b is -1 after casting. Due to this extensive loss of information, the compiler requires you to cast explicitly so that you will never inadvertently loose information.

Primitive casting rules

Though you can change the type forcibly with casting, it does not mean that you can change any type to any other type. There are still certain checks that Java performs before allowing the casting. The following rules particularly govern the casting in primitives:

 

1.       You can cast any non-boolean primitive to any non-boolean primitive type.

You can cast the non-boolean primitives without any restriction. All of the following type castings are valid and compile successfully.

 

double d = 8.5768;

byte b = (byte) b;      // double to byte casting

char c = (char) d;      // double to char casting

short s = (short) c;    // char to short casting

 

You can cast the wider data type double to byte. You can even cast char value to a short. The char and short type are otherwise incompatible.

 

2.       You cannot cast a boolean to any other type than boolean.

The boolean primitives can be cast to boolean only. Such casting is not necessary but can be done.  Following code will compile:

 

boolean b = (boolean) true;   // boolean to boolean casting

 

Remember that you cannot cast any non-boolean type to a boolean. Following code will fail to compile:

 

double d = 1.0;

boolean b = (boolean) d;      // Compile time Error!

 

You cannot cast double value d to the boolean type. The boolean primitives are incompatible with the rest of the primitives and can not be converted or cast.

 

Primitive casting for readability

Casting is necessary while narrowing the data types. However, you can also use casting while widening. Though such use is not required, it can make code more readable. For example, following code makes it explicitly clear that sqrt() method accepts a double value.

 

Math.sqrt( (double) 2);   // casting in a method call

 

In the above example, the cast operator is used to change a narrower data type int into a wider data type double . The casting of 2 is legal but is not necessary.  It makes explicitly clear that you need to pass double value to the sqrt() method.