Type Casting in Java

The process of converting the value of one data type to another data type is known as type casting.

Widening Casting

Converting a smaller type to a larger type size

byte -> short -> char -> int -> long -> float -> double

Byte to Short

Convert byte variable to short variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		byte b = 127;
		short s = b;
		System.out.println("short: " + s);
	}

}
short: 127
 

Short to Char

Convert short variable to char variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		short s = 65;
		char c = (char) s;
		System.out.println("char: " + c);
	}

}
char: A
 

Char to Int

Convert char variable to int variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		char ch = 'B';
		int i = ch;
		System.out.println("i: " + i);
	}

}
i: 66
 

Int to Long

Convert int variable to long variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int i = 123;
		long lo = i;
		System.out.println("lo: " + lo);
	}

}
lo: 123
 

Long to Float

Convert long variable to float variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		long lo = 123;
		float f = lo;
		System.out.println("f: " + f);
	}

}
f: 123.0
 

Float to Double

Convert float variable to double variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		float f = 4.5f;
		double d = f;
		System.out.println("d: " + d);
	}

}
d: 4.5

Best Desktops For Programming

Narrowing Casting

Converting a larger type to a smaller size type.

double -> float -> long -> int -> char -> short -> byte

Double to Float

Convert double variable to float variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		double d = 5.6;
		float f = (float) d;
		System.out.println("f: " + f);
	}

}
f: 5.6
 

Float to Long

Convert float variable to long variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		float f = 5.6f;
		long lo = (long) f;
		System.out.println("lo: " + lo);
	}

}
lo: 5
 

Long to Int

Convert long variable to int variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		long lo = 456;
		int i = (int) lo;
		System.out.println("i: " + i);
	}

}
i: 456
 

Int to Char

Convert int variable to char variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int i = 71;
		char ch = (char) i;
		System.out.println("ch: " + ch);
	}

}
ch: G
 

Char to Short

Convert char variable to short variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		char c = 'D';
		short s = (short) c;
		System.out.println("s: " + s);
	}

}
s: 68
 

Short to Byte

Convert short variable to byte variable

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		short s = 127;
		byte b = (byte) s;
		System.out.println("b: " + b);
	}

}
b: 127