Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A significant part of programming involves playing around with data. While performing some tasks, situations might arise where the data type has to be changed from one type to another. For example, while calculating the area of a circle using Pi, we get a decimal number, and that needs to be converted to integer if we desire to obtain the answer in integer format.
Since Java is strongly typed, one often needs to convert from one data type to another. The programmer or the compiler can easily do this by using type casting and type conversion concepts.
In Java, there are thirteen types of type Conversions. You can explore more about them by referring to the Java Type Conversion section in the Official Oracle Java Documentation. This blog will dive deep into the concept of type conversion and type casting in Java in detail, along with some examples.
What is Type Conversion?
Type conversion is a process in which the data type is automatically converted into another data type. The compiler does this automatic conversion at compile time. The data type to which the conversion happens is called the destination data type, and the data type from which the conversion happens is called the source data type. If the source and destination data types are compatible, then automatic type conversion takes place.
For type conversion to take place, the destination data type must be larger than the source type. In short, the below flow chart has to be followed.
Flow chart for Type Conversion
This type of type conversion is also called Widening Type Conversion/ Implicit Conversion/ Casting Down. In this case, as the lower data types with smaller sizes are converted into higher ones with a larger size, there is no chance of data loss. This makes it safe for usage.
In type conversion, the source data type with a smaller size is converted into the destination data type with a larger size.
Before looking at an example for the type conversion type, let's see what happens when incompatible data types are given.
public class Main {
public static void main(String[] args) {
int intType = 20;
// Short is of lower data type than int
short shortType = intType;
System.out.println("intType: "+intType);
System.out.println("shortType: "+shortType);
}
}
You can also try this code with Online Java Compiler
Type casting is a process in which the programmer manually converts one data type into another data type. For this the casting operator (), the parenthesis is used. Unlike type conversion, the source data type must be larger than the destination type in type casting. The below flow chart has to be followed for successful type casting.
Flow chart for Type Casting
Type casting is also called Narrowing Type Casting/ Explicit Conversion/ Casting Up. In this case, as the higher data types with a larger size are converted into lower ones with a smaller size, there is a chance of data loss. This is the reason that this type of conversion does not happen automatically.
In type casting, the source data type with a larger size is converted into the destination data type with a smaller size.
In one of the examples covered in type conversion, you might recollect we encountered an error while converting an int data type to short. Now, we will see how to solve that error by using type casting.
Example for Type Casting in Java:
Java
Java
public class Main { public static void main(String[] args) { int intType = 20; // Short is of lower data type than int short shortType = (short)intType; System.out.println("intType: "+intType); System.out.println("shortType: "+shortType); } }
You can also try this code with Online Java Compiler
In Java, widening and narrowing conversions describe how primitive data types can be converted into one another.
Widening conversions (also known as automatic or implicit conversions) occur when a smaller data type is converted to a larger data type. These conversions happen automatically and do not result in data loss.
Narrowing conversions (manual or explicit conversions) happen when a larger data type is converted to a smaller one. These conversions may cause data loss and must be done explicitly by the programmer.
Examples of Widening Conversions:
int a = 10; double b = a; The int value is automatically converted to double.
byte c = 20; int d = c; The byte value is widened to int without explicit casting.
Examples of Narrowing Conversions:
double x = 9.8; int y = (int) x; The fractional part is lost after converting double to int.
int m = 300; byte n = (byte) m; The int value is explicitly cast to byte, which may cause data overflow.
Primitive Types and Their Casting Possibilities
Java’s primitive data types in hierarchical order (from smallest to largest):
byte (1 byte)
short (2 bytes)
char (2 bytes)
int (4 bytes)
long (8 bytes)
float (4 bytes, single-precision)
double (8 bytes, double-precision)
Widening (Automatic) Conversions:
byte → short → int → long → float → double
char → int → long → float → double
These conversions happen automatically because they are safe and there is no risk of data loss.
Narrowing (Explicit) Conversions:
double → float → long → int → short → byte
int → char and long → char require explicit casting
Narrowing conversions can lead to data loss, truncation, or overflow and must be explicitly handled by the programmer.
Conversion Compatibility Table
From \ To
byte
short
char
int
long
float
double
byte
—
Yes
No
Yes
Yes
Yes
Yes
short
No
—
No
Yes
Yes
Yes
Yes
char
No
No
—
Yes
Yes
Yes
Yes
int
No
No
No
—
Yes
Yes
Yes
long
No
No
No
No
—
Yes
Yes
float
No
No
No
No
No
—
Yes
double
No
No
No
No
No
No
—
Yes: Widening (automatic)
No: Requires explicit casting (narrowing)
—: Same data type (no conversion needed)
Difference Between Type Casting and Type Conversion
Parameter
Type Casting
Type Conversion
Definition
Type casting is a process in which the programmer manually converts one data type into another data type.
Type conversion is a process in which the data type is automatically converted into another data type
Method
Explicitly done by the programmer using casting operators.
Can be implicit or explicit, depending on the language and context.
Syntax
(newType) variable
Function calls, constructors, or other conversion methods.
Compatibility
Must be compatible data types.
Can involve conversions between incompatible data types.
Change
Immediate change in the data type of the variable.
The result may involve data loss or rounding errors, depending on the conversion m
Frequently Asked Questions
What is meant by type conversion and type casting in Java?
In type conversion, a compiler converts a lower data type into a higher data type. In typecasting, a higher data type is converted into a lower data type by the programmer.
What are the common methods used to change data types in Java?
Type Conversion and Type Casting in Java enable converting one data type into another.
What is explicit type conversion?
Explicit type conversion is when a higher data type is converted into a lower data type by the programmer explicitly.
What is automatic type conversion?
Automatic type conversion is when a compiler automatically converts a lower data type into a higher data type.
Differentiate between type conversion and type casting in Java based on data loss.
There is a chance of data loss in typecasting, while the data is safe in type conversion.
Conclusion
In conclusion, Type Conversion and Type Casting in Java are essential for handling data across different types. Type conversion (widening) occurs automatically when a smaller data type is assigned to a larger one, while type casting (narrowing) requires explicit conversion. Mastering these ensures accurate computations, optimized memory use, and better control over type safety in Java programs.