Type casting is a technique that is used either by the compiler or a programmer to convert one data type to another in Java. Type casting is also known as type conversion. For example, converting int to double, double to int, short to int, etc.
There are two types of type casting allowed in Java programming:
Widening type casting is also known as implicit type casting in which a smaller type is converted into a larger type, it is done by the compiler automatically.
HierarchyHere is the hierarchy of widening type casting in Java:
byte>short>char>int>long>float>double
The compiler plays a role in the type conversion instead of programmers. It changes the type of the variables at the compile time. Also, type conversion occurs from the small data type to large data type only.
ExampleIn this example, we are adding an integer and a double number, storing the result in a double variable to display the sum, implicitly casting the integer to a double during the addition:
package com.tutorialspoint; public class Tester { // Main driver method public static void main(String[] args) { // Define int variables int num1 = 5004; double num2 = 2.5; double sum = num1 + num2; // show output System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum); } }
Compile and run Tester. This will produce the following result −
OutputThe sum of 5004 and 2.5 is 5006.5Type Conversion Error
The type conversion error occurs when you try to assign a value of a larger data type to a variable of a smaller data type without explicitly converting it.
Java compiler performs implicit type conversion when assigning values between compatible types, but it does not allow the conversion from a larger data type to a smaller one without explicit type casting.
ExampleIn the example below, we demonstrated that we can get an error when the compiler tries to convert a large data type to a small data type. Here, we created the num1 integer and num2 double variable. The sum of num1 and num2 will be double, and when we try to store it to the sum of the int type, the compiler gives an error.
package com.tutorialspoint; public class Tester { // Main driver method public static void main(String[] args) { // Define int variables int num1 = 5004; double num2 = 2.5; int sum = num1 + num2; // show output System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum); } }
Compile and run Tester. This will produce the following result −
OutputException in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from double to int at com.tutorialspoint.Tester.main(Tester.java:9)Narrowing Type Casting
Narrowing type casting is also known as explicit type casting or explicit type conversion which is done by the programmer manually. In the narrowing type casting a larger type can be converted into a smaller type.
When a programmer changes the variable type while writing the code. We can use the cast operator to change the type of the variable. For example, double to int or int to double.
SyntaxBelow is the syntax for narrowing type casting i.e., to manually type conversion:
double doubleNum = (double) num;
The above code statement will convert the variable to double type.
ExampleIn the example below, we define the num variable of integer type and initialize it with the value. Also, we define the doubleNum variable of double type and store the num variable's value after converting it to the double.
Next, We created the 'convertedInt' integer type variable and stored the double value after type casting to int. In the output, we can observe the value of the double and int variables.
package com.tutorialspoint; public class Tester { // Main driver method public static void main(String[] args) { // Define int variable int num = 5004; // Type casting int to double double doubleNum = (double) num; // show output System.out.println("The value of " + num + " after converting to the double is " + doubleNum); // Type casting double to int int convertedInt = (int) doubleNum; // show output System.out.println("The value of " + doubleNum + " after converting to the int again is " + convertedInt); } }
Compile and run Tester. This will produce the following result −
OutputThe value of 5004 after converting to the double is 5004.0 The value of 5004.0 after converting to the int again is 5004
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4