Introduction
Typecasting is a technique in which we convert one type of value into another. In this article, we will learn about typecasting in Java, its types, methods, and constructors provided in Java programming language.
What is Typecasting in Java?
In Java, when you assign a value of one data type to another, the two types may not be compatible. If the data types are compatible, Java will perform the conversion automatically, known as automatic. Typecasting is also known as type conversion in Java. It's done fairly, but sometimes you may get errors.
Typecasting is often necessary when a method returns data of type in a different form than we need to perform an operation.
Under the above certain circumstances, Typecasting can be carried out automatically. In other cases, it must be forced manually (Explicitly).
For example, assigning an int value to a long variable.
Syntax
dataType variableName = (dataType) variableToConvert;
Syntax of Type Casting in Java
The syntax for casting a type is to specify the target type in parentheses, followed by the variable's name or the value to be cast—for example, the following statement.
Syntax
int number;
float value = 32.33;
number= (int) value;
Types of Type Casting in Java
- Implicit Typecasting and Explicit Typecasting
- up-casting and down-casting
- Autoboxing and Unboxing
1) Implicit Typecasting and Explicit Typecasting
Implicit Typecasting in Java
Widening or Automatic Typecasting takes place when two data types are compatible with each other and converts automatically. Casting a type with a small range of a type with a larger range is known as widening Typecasting.
Typecasting from smaller type to larger type size,
byte -> short -> char -> int -> long -> float -> double
- The two data types are compatible.
- When we assign the value of a smaller data type to a bigger data type.
- The destination type is bigger than the source type.
For Example, in Java, the numeric data types are compatible, but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other.
Implicit typecasting is also known as widening typecasting and automatic type conversion in Java.
The complete program of Widening Typecasting is listed below.
public class TypecastingExample3 {
public static void main(String[] args) {
int number = 10;
float point = number;
System.out.println(number);
System.out.println(point);
}
}
The above program generates the following output.

The numeric types, including integer and floating-point types, are compatible with each other.
Note. Although the automatic type conversions are helpful, they will not fulfill all needs. For example, what if you want to assign a float value to a byte variable? This conversion will not be performed automatically because a byte is smaller than a float. This kind of conversion is sometimes called a narrowing conversion since you are explicitly making the value narrower to fit into the target type.
float to int conversion in Java
The complete program for type conversion of float to int is listed below.
public class TypecastingExample1 {
public static void main(String[] args) {
int number;
float fval = 32.33f;
number = (int) fval;
System.out.println(number);
}
}
The above program generates the following output.

int to float conversion in Java
The int and float each take 4 bytes of memory. The int holds a whole number, and the float holds a floating-point number. Assignment of int to float is done automatically by JVM.
public class IntToFloatExample {
public static void main(String args[])
{
int i1 = 10;
float f1 = i1;
System.out.println("int value: " + i1);
System.out.println("Converted float value: " + f1);
}
}
The above program generates the following output.

double to int conversion in Java
The complete program for type conversion of double to int is listed below.
public class TypecastingExample2 {
public static void main(String[] args){
System.out.println((int)1.87);
System.out.println((double)1 / 2);
}
}
The above program generates the following output.

int to double conversion in Java
The complete program of int to double type conversion is listed below.
public class IntToDoubleExample {
public static void main(String args[]) {
int i = 200;
double d = i;
System.out.println(d);
}
}
The above program generates the following output.

double to byte conversion in Java
The complete program of double-to-byte type conversion is listed below.
public class DoubleTOByteExample {
public static void main(String args[])
{
double d1 = 10.5;
byte b1 = d1; //compile time error
byte b1 = (byte) d1; //compile time error
System.out.println("\ndouble value: " + d1);
System.out.println("Converted byte value: " + b1 );
}
}
The above program generates the following output.

Note. When we try to type conversion of double to a byte in Java implicitly, it causes an error the byte data type takes 1 byte of memory, and double takes 8 bytes of memory. Assigning 8 bytes of memory to 1 byte of memory requires explicit casting.
byte to double conversion in Java
The complete program of byte-to-double type conversion is listed below.
public class ByteToDoubleExample {
public static void main(String args[]) {
byte b1 = 10;
double d1 = b1;
System.out.println("byte value: " + b1);
System.out.println("Converted double value: " + d1);
}
}
The above program generates the following output.

The complete program of implicit typecasting is listed below, in which the casting performs automatically.
public class ImplicitConversionExample {
public static void main(String args[]) {
byte i = 50;
// No casting needed for below conversion
short j = i;
int k = j;
long l = k;
float m = l;
double n = m;
System.out.println("byte value : " + i);
System.out.println("short value : " + j);
System.out.println("int value : " + k);
System.out.println("long value : " + l);
System.out.println("float value : " + m);
System.out.println("double value : " + n);
}
}
The above program generates the following output.
Implicit Casting of a Class Type in Java
When we are assigning a smaller type to a larger type, there is no need for casting required. The same applies to the class type, as well. The complete program is listed below.
class Parent {
public void disp() {
System.out.println("Parent disp called");
}
}
public class ImplicitTypecastingExample2 extends Parent {
//child class
public static void main(String args[]) {
Parent p = new ImplicitTypecastingExample2();
p.disp();
}
}
The above program generates the following output.














Join the conversation! Your thoughts help the community grow.