Introduction

 

Various aspects of the Java programming language are handled by a variety of data types. Unfortunately, we have to convert one sort of data to another more often. Java's type-casting feature is useful and effective in this situation. Type casting is a technique or process used in Java that effectively converts one data type into another. It is either manually or automatically. The compiler performs the automated conversion, and the programmer handles the manual conversion. The type casting Java feature allows the form or type of a variable or object to be cast into a different kind of object. In simple words, type casting is the process of converting one type to another.


Here is a quick guide to explain type casting in Java. Let's examine typecasting with appropriate examples,



What Is Java Type Casting?

 

The Java programming language is renowned for its wide range of capabilities. It can effectively manage many data types. To move forward, we may need to convert one type of data to another. The Java idea of typecasting is applicable in this circumstance. Java's type-casting feature can be used to change one data type into another. We will examine Java-type casting in depth with the help of examples.

 

Type conversion is another name for Java type casting. It is a procedure that helps programmers in giving another primitive data type a value. It is necessary to determine whether a data type is compatible with the one that has been assigned. If the data type is incompatible, type casting in Java must be used to change the data type to one that is.

 

Java-type casting can also be used to convert classes or interfaces into different classes. Java is one of the most adaptable object-oriented programming languages as it supports polymorphism and inheritance. 


Let us take a closer look at the various data types that are accessible in the Java Programming language before delving into the specifics of typecasting.



 

Our Learners Also Read: Is Java Developer A Good Career?

 

 

Java Data Types

 

As a statically typed language, Java requires that variables be declared before usage. Below are the main types of data in Java:-

 

1. Primitive Data Type

 

It is the most basic type of data with 8 types. In the integer category, there is a byte, short, long, and int data type. The rest are char, float, boolean, and double data types. 



2. Non-primitive Data Type

 

Non-primitive data types have corresponding methods, in contrast to primitive data types, which don't. The Objects are referred to. It is also known as Reference Data Types or Object Data Types. It has sub-categories like an array, string, class, and others. 



 

Casting In Java Types

 

In a nutshell, there are two different casting types in the Java programming language. which are:-



1. Widening Type Casting

 

Widening typecasting is the process of transforming a lower data type into a higher one. It is also referred to as implicit conversion or casting down and happens automatically. The data type need not be specified explicitly. It is secure since there is no possibility of data loss. Widening type casting is feasible when both data types are compatible with one another and the destination type is greater than the source type.

 

The succession of data types from smaller to greater values/capacities is shown below. On these data types, more inclusive type casting is feasible.

 

(byte) --> (short) --> (char) --> (int) --> (long) --> (float) --> (double)

 

See the example below of widening type casting. It converts an int type into a floating type without explicitly mentioning the type.

 

// class

public class Main { 

 

   // main method

   public static void main(String[] args) { 

 

       // variable type int

       int x = 5; 

 

       //convert the integer type into a long type 

       float y = x; 

 

       // print the type casting before and after in java

       System.out.println("Before conversion: "+x); 

       System.out.println("After conversion : "+y); 

   }

 

Output:-

 

Before conversion: 5

After conversion: 5.0

 

You can see that the value was an int type before casting but becomes a float type after casting.



 

2. Narrowing Type Casting

 

Narrowing type casting refers to the transformation of a higher data type into a lower one. Casting up and explicit conversion are other names for it. The programmer does it by hand. The compiler will raise a compile-time error if the casting is not done. We manually transform one data type into another in Narrowing Type Casting using the parentheses.

 

A progression of data types from larger to lower values/capacities is shown below. On these data types, more restrictive type casting is feasible.

 

(double) –> (float) –> (long) –> (int) –> (char) –> (short) –> (byte)

 

Let's look at an example where the narrowing type casting technique is applied.  See how it converts a float data type to an integer. Look at the illustration below:

 

// class

public class Main { 

 

   // main method

   public static void main(String[] args) { 

 

       // variable type float

       float x = 5.61f; 

 

       // Explicitly mentioned the data type 

       int y = (int)x; 

 

       // print the before and after java type casting

       System.out.println("Before conversion: "+x); 

       System.out.println("After conversion : "+y); 

   } 

 

Output:-

 

Before conversion: 5.61

After conversion: 5

 

You will see that it was a floating-point number before the type was cast, and we now have an integer value. Even though we lost some of the data, typecasting can still result in data loss.



 

Conclusion

 

Java type casting is involved in translating one data type into another either manually or automatically. The compiler performs the automated conversion, and the programmer handles the manual conversion. We taught about Java typecasting in this tutorial along with examples of typecasting. We also saw several forms of data types that the Java Programming Language offers and typecast those data types. Additionally, we learned about the two most significant casting types: widening casting and narrowing casting. In a nutshell, this lesson covers every aspect of typecasting in Java.

 

For more information on Java, join the Full Stack Java Developer Course offered by E & ICT Academy IIT Guwahati. By the end of the course, you will grasp Java concepts thoroughly.

 

full stack java developer course