Primitive Numeric Type Conversions Tutorial

In Java, we can declare a variable any of the following 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
This tutorial will focus on what happens when we assign the values of variables of different data types to each other. In order to simply assign the value of a variable of one data type to the variable of another data type the receiving variable needs to be compatible. To be compatible, generally the variable to the left of the assignment operator (=) must be the same size or larger than the variable on the right. The char, float, and double data type variables do not adhere to this generalization though.
The following code will detail the process of what is technically called widening primitive conversions. There are 19 valid conversions as you will see in the source code. My tutorial on Primitive Numeric Type Casting will discuss how you can assign values to incompatible data types.



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 TypeConversion
C:\Java>cd TypeConversion
C:\Java\TypeConversion>Notepad TypeConversion.java

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


class TypeConversion {
	public static void main(String args[]) {
		byte b = 127, b1 = 0;
		short s = 32_767, s1 = 0;
		char c = 65_535, c1 = 0;
		int i = 2_147_483_647, i1 = 0;
		long L = 9_223_372_036_854_775_807L, L1 = 0;
		float f = 2_147_483_646.99F, f1 = 0.0F;
		double d = 9_223_372_036_854_775_806.99D, d1 = 0.0D;
		
		// long section
		System.out.println("----long section----");
		L1 = i;
		System.out.println("Assignment from int: "+L1);
		L1 = c;
		System.out.println("Assignment from char: "+L1);
		L1 = s;
		System.out.println("Assignment from short: "+L1);
		L1 = b;
		System.out.println("Assignment from byte: "+L1);
		System.out.println("");

		// int section
		System.out.println("----int section----");
		i1 = c;
		System.out.println("Assignment from char: "+i1);
		i1 = s;
		System.out.println("Assignment from short: "+i1);
		i1 = b;
		System.out.println("Assignment from byte: "+i1);
		System.out.println("");

		// short section
		System.out.println("----short section----");
		s1 = b;
		System.out.println("Assignment from byte: "+s1);
		System.out.println("");

		// double section
		System.out.println("----double section----");
		d1 = L;
		System.out.println("Assignment from long: "+d1); 
		d1 = L;
		System.out.println("Assignment from long: "+(long)d1);
		d1 = f;
		System.out.println("Assignment from float: "+d1);
		d1 = i;
		System.out.println("Assignment from int: "+d1);
		d1 = c;
		System.out.println("Assignment from char: "+d1);
		d1 = s;
		System.out.println("Assignment from short: "+d1);
		d1 = b;
		System.out.println("Assignment from byte: "+d1);
		System.out.println("");

		// float section
		System.out.println("----float section----");
		f1 = i;
		System.out.println("Assignment from int: "+f1); // defaults to scientific notation
		f1 = i;
		System.out.println("Assignment from int: "+(int)f1);
		f1 = c;
		System.out.println("Assignment from char: "+f1);
		f1 = s;
		System.out.println("Assignment from short: "+f1);
		f1 = b;
		System.out.println("Assignment from byte: "+f1);
		System.out.println("");
	}
}

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


C:\Java\TypeConversion>javac TypeConversion.java
C:\Java\TypeConversion>java TypeConversion
----long section----
Assignment from int: 2147483647
Assignment from char: 65535
Assignment from short: 32767
Assignment from byte: 127

----int section----
Assignment from char: 65535
Assignment from short: 32767
Assignment from byte: 127

----short section----
Assignment from byte: 127

----double section----
Assignment from long: 9.223372036854776E18
Assignment from long: 9223372036854775807
Assignment from float: 2.147483648E9
Assignment from int: 2147483647
Assignment from char: 65535.0
Assignment from short: 32767.0
Assignment from byte: 127.0

----float section----
Assignment from int: 2.14748365E9
Assignment from int: 2147483647
Assignment from char: 65535.0
Assignment from short: 32767.0
Assignment from byte: 127.0



Final thoughts

As you can see, there are many assignment operations that are performed automatically based on the data type. My tutorial on Primitive Numeric Type Casting will discuss how you can assign values to incompatible data types.


Tutorials