0% found this document useful (0 votes)
1 views

Java Type Casting

The document explains Java type casting, a technique for converting one data type to another, which includes widening (implicit) and narrowing (explicit) type casting. Widening type casting automatically converts smaller types to larger types, while narrowing type casting requires manual conversion by the programmer. It also highlights potential type conversion errors when assigning larger data types to smaller ones without explicit casting.

Uploaded by

Foued
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java Type Casting

The document explains Java type casting, a technique for converting one data type to another, which includes widening (implicit) and narrowing (explicit) type casting. Widening type casting automatically converts smaller types to larger types, while narrowing type casting requires manual conversion by the programmer. It also highlights potential type conversion errors when assigning larger data types to smaller ones without explicit casting.

Uploaded by

Foued
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

10/04/2025 22:57 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

Java Type Casting (Conversion)


Add the Microsoft Rewards extension and
start earning rewards for doing what you DOWNLOAD
love.

Java Type Casting


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

Narrowing type casting

Advertisement
-

Widening Type Casting


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.

Hierarchy

Here 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.

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;

public class Tester {

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

The sum of 5004 and 2.5 is 5006.5

Type 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.

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;

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 −

Output

Exception 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.

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:

double doubleNum = (double) num;

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;

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 −

Output

The value of 5004 after converting to the double is 5004.0


The value of 5004.0 after converting to the int again is 5004

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

Cloud Computing Tutorial


Amazon Web Services Tutorial

Microsoft Azure Tutorial


Git Tutorial
Ethical Hacking Tutorial
Docker Tutorial

Kubernetes Tutorial
DSA Tutorial
Spring Boot Tutorial
SDLC Tutorial

Unix Tutorial

CERTIFICATIONS

Business Analytics Certification

Java & Spring Boot Advanced Certification


Data Science Advanced Certification
Cloud Computing And DevOps

Advanced Certification In Business Analytics


Artificial Intelligence And Machine Learning
DevOps Certification
Game Development Certification

Front-End Developer Certification


AWS Certification Training
Python Programming Certification

COMPILERS & EDITORS

Online Java Compiler


Online Python Compiler
Online Go Compiler

Online C Compiler
Online C++ Compiler
Online C# Compiler

Online PHP Compiler


Online MATLAB Compiler
Online Bash Compiler
Online SQL Compiler

Online Html Editor

ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE | PRIVACY POLICY | REFUND POLICY |

COOKIES POLICY | FAQ'S

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

You might also like