×
☰ See All Chapters

Type Casting in Java

Automatic Type Casting/Promotion:

When one type of data is assigned to another variable of different type, an automatic type casing/conversion will take place if the following two conditions are met:

  1. The two types are compatible. 

  2. The destination type is larger than the source type. 

When these two conditions are met, a widening conversion takes place.

type-conversion-and-casting-0
 

class TypeCasting {

        public static void main(String[] args) {

                System.out.println("Hello World!");

                int a=10;

                float f;

                f=a;

                System.out.println(f); // 10.0  is printed

        }

}

 

Java performs Integer arithmetic at int level. All expressions are evaluated and result will be of type int.  Java defines several type promotion rules that apply to expressions. They are as follows: First, all byte, short, and char values are promoted to int, as just described. Then, if one operand is a long, the whole expression is promoted to long. If one operand is a float, the entire expression is promoted to float. If any of the operands is double, the result is double.

type-conversion-and-casting-1
 

class Promote {

        public static void main(String args[]) {

                byte b = 42;

                char c = 'a';

                short s = 1024;

                int i = 50000;

                float f = 5.67f;

                double d = .1234;

                double result = (f * b) + (i / c) - (d * s);

                System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));

                System.out.println("result = " + result);

        }

}

Let’s look closely at the type promotions that occur in this line from the program:

double result = (f * b) + (i / c) - (d * s);

In the first sub expression, f * b, b is promoted to a float and the result of the sub expression is float. Next, in the sub expression i / c, c is promoted to int, and the result is of type int. Then, in d * s, the value of s is promoted to double, and the type of the sub expression is double. Finally, these three intermediate values, float, int, and double, are considered. The outcome of float plus an int is a float. Then the resultant float minus the last double is promoted to double, which is the type for the final result of the expression.

class TypeCasting {

        public static void main(String[] args) {

                System.out.println("Hello World!");

                byte a = 10, b = 20, c;

                int i;

                /*

                * c=a+b; this is not possible, because result of a+b is int and we can't store

                * it in byte

                */

                i = a + b; // valid

                System.out.println("value of i=" + i);

        }

}

Casting Incompatible Types

type-conversion-and-casting-2
 

Remember the fact a mug can go inside the tub, but tub can’t go inside the mug, in same way if we want to send larger data type to smaller data type we need a casting. It has this general form.

(target-type) value;

Casting Incompatible Types Example

int a = 10;

byte b;

b = (byte) a;

Here, target-type specifies the desired type to convert the specified value to. For example, the following 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.

class TypeCasting {

        public static void main(String[] args) {

                byte b;

                int i = 257;

                double d = 323.142;

                System.out.println("\nConversion of int to byte.");

                b = (byte) i;

                System.out.println("i and b " + i + " " + b);

                System.out.println("\nConversion of double to int.");

                i = (int) d;

                System.out.println("d and i " + d + " " + i);

                System.out.println("\nConversion of double to byte.");

                b = (byte) d;

                System.out.println("d and b " + d + " " + b);

        }

}

This program generates the following output:

Conversion of int to byte.

i and b 257 1

Conversion of double to int.

d and i 323.142 323

Conversion of double to byte.

d and b 323.142 67

 

Let’s look at each conversion. 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 the 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 in this case is 67.

 


All Chapters
Author