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.
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.
Developers use primitive data types to assign to other primitive data types. There are seven types of primitive data type values, such as –
Other than the values you can cast to primitive data types, there are two subtypes to learn – implicit and explicit 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;
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 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
double d = 150.06;
//explicit type casting
long l = (long)d;
int i = (int)l;
System.out.println(“Double Data type value “+d);
//fractional part lost
System.out.println(“Long Data type value “+l);
System.out.println(“Int Data type value “+i);
Double Data type value 150.06
Long Data type value 150
Int Data type value 150
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 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 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.
Here, we have explored different casting types in Java and their usage to establish relationships between other classes and data types.
Fill in the details to know more
What Is the Use of Wrapper Class in Java?
March 22, 2023
What Is Clean Coding in Java?
March 21, 2023
What Are the New Features of Java 17?
What Is File Handling in Java?
March 16, 2023
What Is Data and Time Function in Java?
March 11, 2023
Top 10 Emerging Technologies Blogs To Read In 2023
December 15, 2022
History of Java Programming Language| UNext
November 18, 2022
An Advanced Guide to Shell Scripting in Unix!
November 15, 2022
Java Tutorial For Beginners
September 28, 2022
What Is Data Abstraction?
August 24, 2022
Encapsulation In Oops With Example
August 23, 2022