English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn Java Type Conversionand their types.
While learningBefore Java type conversion, make sure you understandJava Data Types.
The process of converting a value of one data type (int, float, double, etc.) to another data type is called type conversion.
In Java, there are13of type conversion. However, in this tutorial, we will only focus on the main two types.
1, Type conversion
2, Automatic type conversion
In, Narrowing conversionIn automatic type conversion
class Main { public static void main(String[] args) { //Create an int type variable int num = 10; Example: Convert int to double + num); //Converted to double type double data = num; System.out.println("Double value: ", + data); } }
Output Result
Integer value: 10 Double value: 10.0
In the above example, we assign the int type variable named num to the double type variable named data.
Here, Java first converts the int type data to double type. Then it assigns it to the double variable.
InAutomatic type conversionIn the case, a lower data type (with a smaller size) will be converted to a higher data type (with a larger size). Therefore, there is no data loss. This is why this type of conversion happens automatically.
Note: This is also calledImplicit type conversion.
InType castingIn, we use parentheses to manually convert one data type to another.
class Main { public static void main(String[] args) { //Create a double type variable double num = 10.99; System.out.println("Double value: ", + num); //Converted to int type int data = (int)num; System.out.println("Integer value: ", + data); } }
Output Result
Double value: 10.99 Integer value: 10
In the above example, we assign the double variable named num to the int variable named data.
Note this line,
int data = (int)num;
Here, the int keyword in the parentheses indicates that the num variable is converted to the int type.
InType castingIn the case, a higher data type (with a larger size) will be converted to a lower data type (with a smaller size). Therefore, there is data loss. This is why this type of conversion does not happen automatically.
Note: This is also calledExplicit type conversion.
Let's look at some other type conversion examples in Java.
class Main { public static void main(String[] args) { // Create an int type variable int num = 10; System.out.println("Integer value is: "); + num); //Convert int to string type String data = String.valueOf(num); System.out.println("String value is: "); + data); } }
Output Result
Integer value is: 10 String value is: 10
In the above program, note this line
String data = String.valueOf(num);
Here, we useJava String classThe valueOf() method converts an int type variable to a string.
class Main { public static void main(String[] args) { //Create a string type variable String data = "10"; System.out.println("String value is: "); + data); //Convert string variable to int int num = Integer.parseInt(data); System.out.println("Integer value is: "); + num); } }
Output Result
String value is: 10 Integer value is: 10
In the above example, please note the following line
int num = Integer.parseInt(data);
In this example, we used the parseInt() method of the Java Integer class to convert a string type variable to an int variable.
NoteIf a string variable cannot be converted to an integer variable, a NumberFormatException will be thrown.