In Java, just like most programming languages, we can convert one data type into another type using Type Conversion. Mainly, there are two types of conversions, and in this example, we’ll be using both of them. First one is automatic type conversion, which automatically converts compatible data types into another type using Java — for example, converting an integer into a float or a double. Java internally does this process for you.

Java Datatype Conversions

Another type is explicit typecasting, where the programmer has to cast to the resulting data type by manually. For example; converting a double value to an integer or a long type. In that case, the precision will be lost as we try to a store bigger value in a smaller data format. This conversion type is typically used in conversions between incompatible data types.

In this article, we’ll be converting an integer into a byte, float and char formats using Java.

public static void main(String[] args) {
   //get user input
   System.out.print("Enter an integer to convert: ");
   Scanner input = new Scanner(System.in);
   int number = input.nextInt();
	    
   //explicit casting
   byte byteConversion = (byte)number;  

   //explicit casting
   char charConversion = (char)number;
   
   //automatic type conversion
   float floatConversion = number;
	    
   //print statements
   System.out.println("Conversion of "+ number +" into Byte: "
                             + byteConversion);
   System.out.println("Conversion of "+ number +" into Character: "
                             + charConversion);
   System.out.println("Conversion of "+ number +" into Float: "
                             + floatConversion);
   input.close();
}

Code Explanation

First, we take the input from the user and convert it to an integer by nextInt() method. After that, an integer is explicitly converted to byte and character. In byte conversion, it can hold values in between -128 and 127 only. When the input number exceeds that range, it subtract that number from 28(256) until that number falls in the above range. Integers are represented in 32 bits and bytes are typically represented in 8 bits. When converting the integer into character, it returns with the particular ASCII character equivalent to the integer used. Both these methods are fallen under the explicit type casting.

Converting an integer into a float or a double always happens automatically as an integer is smaller than the float and a double. Therefore casting is not needed in the conversion. In float conversion, it will add precision to the integer number we used. After all these conversions, converted values are displayed through the print statements as shown below.

Result:

Enter an integer to convert: 99
Conversion of 99 into Byte: 99
Conversion of 99 into Character: c
Conversion of 99 into Float: 99.0

 

Enter an integer to convert: 120
Conversion of 120 into Byte: 120
Conversion of 120 into Character: X
Conversion of 120 into Float: 120.0

Recommended Reading

Last modified: March 18, 2019