Summary: in this tutorial, you’ll learn about Java type conversion which allows you to convert a value of one type to another.
Introduction to Java type conversion
Java has a number of different primitive types. For example, integer types include byte
, short
, int
, and long
. Sometimes, you need to convert a value of one type to another.
Generally speaking, type conversion has two broad categories:
- Implicit type conversion
- Explicit type conversion
Implicit type conversion
Implicit type conversion occurs when you convert a value of a type with a smaller range to a value of a type with a larger range. Implicit type conversion is performed automatically by the Java compiler.
If you convert a value of type from left to right, you need to perform an explicit type conversion:
byte -> short -> char -> int -> long -> float -> double
For example:
int count = 1;
long countBig = count;
Code language: Java (java)
In this example:
- First, define a variable
count
with typeint
and initialize its value to1
. - Second, define another variable
countBig
with the typelong
and assign the value of thecount
variable to it.
Because the type of the count
is int
and countBig
is long
, the Java compiler automatically converts the value of the count
variable to long
and assigns it to the countBig
variable.
The implicit type conversion is also known as implicit casting or widening casting. Java allows this type of conversion because it doesn’t result in any data loss.
Explicit type conversion
Explicit type conversion occurs when you convert a value of one type with a larger range to another type with a smaller range.
The explicit type conversion is also known as explicit casting or narrowing casting. Because the explicit type conversion may result in data loss or truncation of value, Java requires a cast operator.
If you convert a value of type from left to right, you need to perform an explicit type conversion:
double -> float -> long -> int -> char -> short -> byte
The syntax for explicit type conversion is as follows:
targetType variable = (targetType)anotherVariable;
Code language: Java (java)
In this syntax, the (targetType)
is the cast operator. For example:
long countBig = 1;
int count = (int) countBig;
Code language: Java (java)
In this example:
- First, define a variable
countBig
with typelong
and initialize its value to 1. - Second, define another variable
count
with the typeint
and assign the value of thecountBig
variable to thecount
variable.
The (int)
is the cast operator that converts the value of the long
type to a value of the int
type.
Java type conversion examples
Let’s take some examples of using type conversions in Java.
1) Using an explicit type conversion that doesn’t cause data loss
public class App {
public static void main(String[] args) {
int x = 10;
long y = 20;
int z = x + y; // error
System.out.println(z);
}
}
Code language: Java (java)
In this example:
- First, define two variables
x
with typeint
andy
with typelong
. - Second, define another variable
z
with typeint
, add the values ofx
andy
, and assign the result to thez
variable.
Because we sum an int
to a long
values, the result will have the type long
. However, since we assign a value of type long
to a variable of type int
, the program won’t be compiled.
To make it work, you need to change the type of the variable z
to long
or perform an explicit type conversion:
public class App {
public static void main(String[] args) {
int x = 10;
long y = 20;
int z = (int)(x + y);
System.out.println(z);
}
}
Code language: Java (java)
2) Using an explicit type conversion that causes a truncation of value
The following example shows how to explicitly convert a value of the float type to a value of the int type which results in data loss:
public class App {
public static void main(String[] args) {
double price = 10.99;
int price2 = (int)price;
System.out.println(price2);
}
}
Code language: Java (java)
In this example:
- First, define the variable price with type double and initialize its value to
10.99
. - Second, define another variable price2 with the type int and assign the value of the price variable to it.
Since the type of the price has a larger range than the type of the price2, we need to use the cast operator (int).
In this example, the (int) operator truncates the fractional part of the value of the price variable.
Summary
- Java type conversion allows you to convert a value of one type to another.
- Java has explicit type conversion (widening cast) and implicit type conversion (narrowing cast).
- Widening conversion is safe and automatic because it doesn’t cause any data loss while the narrowing conversion should be done with care to ensure that data loss is acceptable in your specific use case.