Introduction

Are you a beginner looking to be a prominent Java developer? Then here is everything you should know about type casting in Java. Whether you are a student or a professional looking to enhance your programming knowledge, Java is the first thing to mind. It is one of the most famous object-oriented programming languages. A Java compiler can help compile the Java code into bytecodes and benefit powerful Java Virtual Machines or JVM to convert it to the machine code.

As per sources, on average, a Java developer can be offered $5411.37 per year, so many upcoming programmers and students choose Java learning programs. If you are one of them, then there is one specific practice in Java, a must-type casting. Let us try to explain type casting in Java with example programs.

  1. What Is Type Casting In Java?
  2. Types of Type Casting In Java

1. What Is Type Casting In Java?

Typecasting, also known as type conversion in Java, is a process that helps developers to assign a primitive data type value to other primitive data types. Here, compatibility is the key! Developers need to check whether a data type is compatible with the assigned data type or not. If both the data types are compatible, typecasting is automatically performed and popularly known as automatic type conversion.

Type conversion and casting in Java is also a process 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.

For example, there can be instances in Java where a subclass represents a superclass. The compiler may go unnoticed during the compilation. So, it becomes necessary to cast the subclass object in Java into a type that initially existed. But, is it enough to know the typecasting? Apart from the basics of typecasting, you should be aware of the Java rules around it.

Now, let’s discuss different types of type casting in Java.

2. Types of Type Casting In Java

  • Primitive Type Casting In Java

Developers use primitive data types to assign to other primitive data types. There are seven types of primitive data type values, such as –

  • Boolean
  • Byte
  • Char
  • Short
  • Int
  • Long
  • Float
  • Double

Other than the values you can cast to primitive data types, there are two subtypes to learn – implicit and explicit type casting in Java.

  • Implicit Type Casting In Java

A widening or implicit casting is all about casting a data type with lower values than the data type with higher values without data loss. It can be difficult for a developer to use implicit casting methods because there lies a significant risk of casting a lower value than the higher value. It can even lead to a small loss of data.

Here is an implicit type casting example in Java –

public class Conversion{

public static void main(String[] args)

{

int i = 150;

//automatic type conversion

long l = i;

//automatic type conversion

float f = l;

System.out.println(“Int value “+i);

System.out.println(“Long value “+l);

System.out.println(“Float value “+f);

}

}

Output:

Int value 150

Long value 150

Float value 150.0

  • Explicit Type Casting In Java

Explicit casting is just the opposite of the widening approach. So, instead of assigning a lower value here, the developer gives a higher value. The narrower data type or the one with a lower value is set with a higher value and requires careful executions to avoid data loss. While an implicit conversion performs automatically, an explicit one takes the prowess of developer input.

Here is an explicit type casting example in Java –

public class Narrowing

{

public static void main(String[] args)

{

double d = 150.06;

//explicit type casting

long l = (long)d;

//explicit type casting

int i = (int)l;

System.out.println(“Double Data type value “+d);

//fractional part lost

System.out.println(“Long Data type value “+l);

//fractional part lost

System.out.println(“Int Data type value “+i);

}

}

Output:

Double Data type value 150.06

Long Data type value 150

Int Data type value 150

  • Reference Type Casting In Java

If two different class types are associated with each other through inheritance, one will be a subclass. It is the condition for any class to undergo casting and is essential to ensure developers follow Java run-time rules. Reference type casting is divided into two subtypes.

  • Upcasting

Upcasting is all about the conversion of a subtype object into a supertype object. It leverages the fantastic ability of Java of non-provisioning for assigning the object without the need for explicit casting. The compiler will now automatically cast a subtype value to a supertype object.

  • Downcasting

Downcasting is a casting approach that helps developers to convert supertype to subtype objects. Here, developers can communicate to the compiler that the base object’s value gets assigned to the other supertype object.

Conclusion

Here, we have explored different casting types in Java and their usage to establish relationships between other classes and data types.

Also Read

SHARE