Primitive Numeric Type Casting Tutorial

There will be situations when you need to assign one data type the value of an incompatible data type. If you watched my tutorial on Primitive Numeric Type Conversions, then you learned about variable assignments of compatible data types. Incompatible data types can be assigned to one another using a cast. A cast tells the javac compiler to convert one type into the other; by doing so, there may be loss of data. You are telling the compiler that you know best and to ignore warnings and compile away. It works like this:
(variable of target data type) = (cast of (target data type)) (variable, literal, expression, or method result of incompatible data type)
double d = 12.99;
int a = (int) d; //cast double to type int
System.out.println(a); // will display 12, we lost the .99 portion of the double value


For reference, here are the numeric primitive data types:

  • byte · 8-bit integer, -128 to 127
  • short · 16-bit integer, -32,768 to 32,767
  • int · 32-bit integer, -2147483648 to 2147483647
  • long · 64-bit integer, -9223372036854775808L to 9223372036854775807L
  • float · 32-bit single-precision floating point
  • double · 64-bit double-precision floating point
  • char · 16-bit unsigned Unicode character, 0 to 65,535



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md TypeCasting
C:\Java>cd TypeCasting
C:\Java\TypeCasting>Notepad TypeCasting.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class TypeCasting {
	public static void main(String args[]) {
		byte b = 0;
		short s = 0;
		char c = 0;
		int i = 0;
		long L = 0;
		float f = 0.0F;
		double d = 50_000.99D;

		b = (byte) d;
		s = (short) d;
		c = (char) d;
		i = (int) d;
		L = (long) d;
		f = (float) d;
	
		System.out.println("double d value: "+d);
		System.out.println("cast (byte): "+b);
		System.out.println("cast (short): "+s);
		System.out.println("cast (char): "+c);
		System.out.println("cast (int): "+i);
		System.out.println("cast (long): "+L);
		System.out.println("cast (float): "+f);
		
		System.out.println();
		c = 'A';
		i = (int) c;
		System.out.println("ASCII A is decimal " + i);
                System.out.println();
		b = (byte) 100.45; // cast on a literal, legal but stupid.
		System.out.println("b = " + b);
        	System.out.println();
		s = (short) (i*2); // cast on an expression - special rules
		System.out.println("s = " + s);
	}
}

Now switch back to the command prompt (CMD) and type in javac TypeCasting.java and press Enter.
Now type in java TypeCasting and press Enter.


C:\Java\TypeCasting>javac TypeCasting.java
C:\Java\TypeCasting>java TypeCasting
double d value: 50000.99
cast (byte): 32
cast (short): -24288
cast (char): ?
cast (int): 50000
cast (long): 50000
cast (float): 50000.99

ASCII A is decimal 65

b = 100

s = 130


Final thoughts

My previous tutorial on Primitive Numeric Type Conversions, this tutorial, and my Primitive Numeric Type Promotion tutorial all tie together. The Primitive Numeric Type Promotion tutorial will explain how arithmetic operations are performed using type promotion. It is very helpful to have a solid grasp of how conversion, casting, promotion, and the arithmetic operators all correspond with each other.


Tutorials