Introduction to Java type casting
Java programming language is well known for its diverse features that are efficiently handled by numerous data types. Sometimes we are required to convert one type of data to another in order to proceed. In such a situation, the concept of Type casting in Java comes into play. Java Type Casting is a feature in Java that is used to cast or convert one data type to another. In this section, we will have a deep look at java type casting.
We will learn how we can convert one data to another. Moreover, we will also give a touch on the different data types that are available in a java programming language. Furthermore, we will discuss the two main type casting methods and will use those methods to convert primitive data types by taking various examples. All in all, this tutorial will cover each and everything that you need to learn in order to get started with java type casting.
Getting started with Java type casting
Java type casting is also known as type conversion. It is a process that helps developers to assign a primitive data type value to other primitive data types. Sometimes, we need to check whether a data type is compatible with the assigned data type or not. If the data type is not compatible then we have to use the java type casting method to convert the data type to a compatible one.
Moreover, Java type casting is also used to cast classes or interface into another class. The support for polymorphism and inheritance in Java makes it one of the most flexible object-oriented programming languages. But before going into the details of java type casting, let us first look at the different data types available in a java programming language.
Data types in Java programming language
Basically, there are two main categories of data types in the Java programming language:
- Primitive data types
- Non-primitive data types
Then they again have sub-categories. Here we will have a look at those different data types.
You can learn more about java data types here. See the diagram below, which shows all the data types and their sub-types.
These are the different data types and their sub-types supported by the java programming language.
Types of casting in Java programming language
Basically, there are two main types of casting in a java programming language. These are;
- Widening type casting
- Narrowing type casting
In the following section, we will discuss the above-mentioned java data casting methods in more detail.
Widening type casting
Converting a lower data type into a higher one is called widening type casting. It is also known as implicit conversion or casting down and it is done automatically, we don't need to explicitly mention the data type. It is safe because there is no chance to lose data. The widening type casting is possible when both data types are compatible with each other and the target type is larger than the source type.
For example, the conversion between numeric data type to char or Boolean is not done automatically. In a similar way, the char and Boolean data types are not compatible with each other.
The following sequence is an example of data types from smaller to larger values/capacities. The widening type casting on these data types is possible.
byte -> short -> char -> int -> long -> float -> double
Now let us take an example of widening type casting. See the example below, which converts and int type into floating type without explicitly mentioning the type.
// class
public class Main {Â
   // main method
   public static void main(String[] args) {Â
       // variable type int
       int x = 7;Â
       // automatically converts the integer type into long type
       float y = x;Â
       // printing the java type casting before and after
       System.out.println("Before conversion: "+x);
       System.out.println("After conversion : "+y);
 }
}
output:
Before conversion: 7
After conversion : 7.0
Notice that before casting the value was an int
type and after casting it becomes float type.
Narrowing type casting
Converting a higher data type into a lower one is called the narrowing type casting. It is also known as explicit conversion or casting up. It is done manually by the programmer which means we have to explicitly mentioned about the data type to be converted. If we do not perform casting then the compiler reports a compile-time error.
The following sequence is an example of data types from larger to smaller values/capacities. The narrowing type casting on these data types is possible.
double -> float -> long -> int -> char -> short -> byte
Now let us take an example that converts the float data type into an integer using the narrowing type casting method. See the example below:
// class
public class Main {Â
   // main method
   public static void main(String[] args) {Â
       // variable type float
       float x = 7.98f;Â
       // Explicitly mentioned about the data type
       int y = (int)x;Â
       // printing the java type casting before and after
       System.out.println("Before conversion: "+x);
       System.out.println("After conversion : "+y);
  }Â
}
Output:
Before conversion: 7.98
After conversion : 7
Notice that before casting the type, it was a floating number and after converting it, we get an integer value. Although we lost some part of the data narrowing typecast can cause us to lose some part of the data.
Java type casting with primitive data type
In the phrase, primitive data type the word primitive means "a fundamental component that is used to create other, larger parts." In java programming, there are a total of 8 different types of primitive data types. These 8 primitive data types are as follows:
byte, short, int, long, float, double, char, boolean
In this section, we will see how we can apply widening and narrowing type casting on primitive data types by taking different examples.
Example of widening typecasting of primitive data type
The process of conversion of a lower data type to a higher data type is known as Widening Typecasting as we already discussed. Java automatically performs this type of casting without any explicit code writing, which is why this type of casting is also known as Automatic typecasting. One important thing is that no data is lost during this conversion. Â this conversion.
Now let us take an example and see how we can perform widening type casting on primitive data types.
// class
public class Main {Â
   // main method
   public static void main(String[] args) {Â
       // variable type byte
       byte x = 4;Â
       // java type casting to short
       short s = x;
       // java type casting to int
       int i = x;
       // java type casting ot floot
       float f = x;
       // jav type casting to double
       double d = x;
       // printing the java type casting before and after
       System.out.println("Before conversion: "+x);
       System.out.println("Converted into short : "+s);
       System.out.println("Converted into int  : "+i);
       System.out.println("Converted into float : "+f);
       System.out.println("Converted into double: "+d);
  }Â
}
Output:
Before conversion: 4
Converted into short : 4
Converted into int : 4
Converted into float : 4.0
Converted into double: 4.0
Notice that we had successfully converted the byte data type into larger data types without explicitly mentioning the type. This is called widening type casting.
Example of narrowing type casting on primitive data type
The process of conversion of higher data type to lower data type is known as narrowing typecasting as we had already discussed. It is not done automatically by Java but needs to be explicitly done by the programmer, which is why it is also called explicit typecasting. And there is a chance of losing the data as well.
Now let us take example of narrowing type casting.
// class
public class Main {Â
   // main method
   public static void main(String[] args) {Â
       // variable type byte
       double x = 4444.0948573837487483;Â
       // java type casting to float
       float f = (float)x;
       // java type casting to int
       int i = (int)x;
       // java type casting to short
       short s = (short)x;
       // jav type casting to byte
       byte b = (byte) x;
       // printing the java type casting before and after
       System.out.println("Before conversion: "+x);
       System.out.println("Converted into float : "+f);
       System.out.println("Converted into int  : "+i);
       System.out.println("Converted into short : "+s);
       System.out.println("Converted into byte: "+b);
  }Â
}
Output:
Before conversion: 4444.094857383749
Converted into float : 4444.0947
Converted into int : 4444
Converted into short : 4444
Converted into byte: 92
Notice that a major part of the original data is lost in each step while narrowing type casting.
Summary
The Java type casting is a method or process that converts a data type into another data type in both ways manually and automatically. The automatic conversion is done by the compiler and the manual conversion is performed by the programmer. In this tutorial, we learned about java type casting along with various examples. We also came across different kinds of data types available in the Java programming language and perform type casting on those data types.
Moreover, we learned about the two most important types of castings;Â widening and narrowing casting. We discussed the differences along with examples and perform each of these casting types on the primitive data types. In a nutshell, this tutorial covers everything about java type casting and we are sure that you will have a depth understanding of type casting after this tutorial.
Further Reading
Java type castingÂ
Java type casting documentation
Java data type documentation