Java Type Casting
Java Type Casting
Home Whiteboard AI Assistant Online Compilers Jobs Tools Articles Corporate Training Teach with us
Chapters Categories
SQL HTML CSS Javascript Python Java C C++ PHP Scala C# Tailwind CSS Node.js MySQL Mongo
Advertisement
-
Hierarchy
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.
Example
In 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:
Open Compiler
package com.tutorialspoint;
https://www.tutorialspoint.com/java/java_type_casting.htm 1/5
10/04/2025 22:57 Java Type Casting
// 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 −
Output
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.
Example
In 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.
Open Compiler
package com.tutorialspoint;
Compile and run Tester. This will produce the following result −
Output
at com.tutorialspoint.Tester.main(Tester.java:9)
https://www.tutorialspoint.com/java/java_type_casting.htm 2/5
10/04/2025 22:57 Java Type Casting
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.
Syntax
Below is the syntax for narrowing type casting i.e., to manually type conversion:
The above code statement will convert the variable to double type.
Example
In 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.
Open Compiler
package com.tutorialspoint;
Compile and run Tester. This will produce the following result −
Output
TOP TUTORIALS
Python Tutorial
Java Tutorial
C++ Tutorial
C Programming Tutorial
C# Tutorial
PHP Tutorial
R Tutorial
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
https://www.tutorialspoint.com/java/java_type_casting.htm 3/5
10/04/2025 22:57 Java Type Casting
SQL Tutorial
TRENDING TECHNOLOGIES
Kubernetes Tutorial
DSA Tutorial
Spring Boot Tutorial
SDLC Tutorial
Unix Tutorial
CERTIFICATIONS
Online C Compiler
Online C++ Compiler
Online C# Compiler
ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE | PRIVACY POLICY | REFUND POLICY |
Tutorials Point is a leading Ed Tech company striving to provide the best learning material on technical and non-technical subjects.
https://www.tutorialspoint.com/java/java_type_casting.htm 4/5
10/04/2025 22:57 Java Type Casting
© Copyright 2025. All Rights Reserved.
https://www.tutorialspoint.com/java/java_type_casting.htm 5/5