Programming is a set of instructions, in a special language, given to the computer to perform a set of tasks. Though many people are using the languages for professional purposes, some developers see it as a way of playing with data and creating functions, solutions, and analysis; they never thought possible.
Students of science are well aware that there are different programming languages, and one of the most prominent ones in Java. No computer engineering student can remain distant from this language. More importantly, professionals can study programming language independently.
Interestingly, Java is a programming language that has several data types to challenge and interest the developers. However, familiarity with the data types is important to play with the data and create instructions as per the end-objective. At the time of coding, a developer may need to change the data types to know how the variable works within the code – this testing of the data variable is known as Type Casting.
In simpler words, computer programmers change one data type to another data type so that a function correctly processes a variable. Typical examples would include converting a floating-point number to an integer, or an integer to a string.
Table of Contents
Understanding Type Casting in Java
Now, that the mechanics of typecasting are clear, it is time to shift attention to type casting in Java.
Typecasting, or type conversion, is the process of assigning a primitive data type’s value to another primitive data type. Programmers need to check the compatibility of the data type they are assigning to another data type, in advance. Typecasting is automatically performed if compatibility between the two data types exists. This type of conversion is called automatic type conversion. On the other hand, in the absence of compatibility between the two data types, then the conversion or casting takes place explicitly.
Typecasting in Java is also the casting of a class or interface into another class or interface. Java supports both polymorphism and inheritance as it is an object-oriented programming language. There could be instances when a SubClass object is pointed as a SuperClass reference variable. However, a Java compiler won’t know this. So, calling a method that has been declared in the SubClass is useless. To make it a useful exercise, you need to first cast that a SubClass object into the type it originally existed as. This is a great example of how type casting in Java works.
Knowing typecasting is not enough as it is equally important to be well aware of the Java rules that need to be followed. One such rule says that interfaces or classes belonging to a single type hierarchy can be converted or cast into one another. If objects without the same type hierarchy or parent-child relationship are converted, a compile-time error to be displayed on your screen.
Let’s look at another scenario you can face as a developer when choosing two objects that belong to the same type hierarchy. But, if the type of the object you are casting and the type of object you are casting it on are not the same, then you will get a ClassCastException.
The most surprising thing is that many developers and programmers are unaware of the importance of typecasting. Typecasting provides access to methods and fields that the target class or type is declared on. There is no other way to access these.
Now, let us look at the different type conversion or casting in Java
There are two types of conversion or casting, namely primitive type casting and reference typecasting.
Types of Casting in Java
Primitive type casting
It allows the developer to cast the value of one primitive into another. The seven primitive data type values are Boolean, Byte, Char, Short, Int, Long, Float and Double. There are two sub-types of primitive type casting:
1. Widening casting or implicit conversion:
Widening casting involves the casting of a data type with a lower value into a data type with a higher value (Widening data type) without any loss of information. It is a big risk in the precision of the casting when considering a wider conversion between different numeric data types. Loss of small bits of information or value is possible.
Also read: Full Stack Project Ideas and Topics
2. Narrowing casting or explicit conversion:
It is the opposite of widening casting. It involves the casting of a data type with a higher value into a data type with a lower value (Narrower data type). If not handled carefully, it can lead to loss of information.
An implicit conversion doesn’t require the developer to provide inputs and is performed automatically. On the other hand, an explicit conversion is performed by the developer alone.
Reference type casting
If two different types of classes are associated with each other by inheritance and one of those classes is the SubClass of another, then these classes can undergo casting. It is important to ensure that the casting is in line with run-time rules as well as compile-time rules in Java. Reference type casting is further divided into two types:
Upcasing involves the conversion of an object of SubType into an object of SuperType. Java has the provision for assigning the object without requiring the addition of an explicit cast. The compiler would know what is being done and would cast the SubType value to SuperType. This way, the object is brought to a basic level. Developers can add an explicit cast without worrying about any issues.
Downcasting involves the conversion of a SuperType object into a Subtype object. It is the most frequently used casting wherein it is communicated to the compiler that the value of the base object isn’t it’s own but of the SuperType object.
To sign off, learning Java typecasting is necessary for becoming a successful developer or programmer. The intent is to define functions and then ensure variables within the communication are performing as per the end-functionality. Knowing and mastering the different types of casting methodologies goes a long way in helping the programmer deliver the most complex functionalities. In direct words, it is a basic which big businesses and brands to improve their end-user engagement.
If you’re interested to learn more about full stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.