Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Typecasting in Java and how does it work?

Last updated on Jun 17,2021 27.6K Views

A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
28 / 72 Blog from Java Core Concepts

Programming is playing around with data. In Java, there are many data types. Most of the times while coding, it is necessary to change the type of data to understand the processing of a variable and this is called Type Casting. In this article, I will talk about the fundamentals of Type Casting in Java.

Below topics are covered in this article:

Let’s get started!

What is Type Casting?

Type casting is nothing but assigning a value of one primitive data type to another. When you assign the value of one data type to another, you should be aware of the compatibility of the data type. If they are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not, then they need to be casted or converted explicitly.

There are two types of casting in Java as follows:

  • Widening Casting (automatically) – This involves the conversion of a smaller data type to the larger type size.

    byte -> short -> char -> int -> long -> float -> double

  • Narrowing Casting (manually) – This involves converting a larger data type to a smaller size type.

    double -> float -> long -> int -> char -> short -> byte

Now let’s get into the details of the types of type casting.

Widening Casting

This type of casting takes place when two data types are automatically converted. It is also known as Implicit Conversion. This happens when the two data types are compatible and also when we assign the value of a smaller data type to a larger data type.

For Example, The numeric data types are compatible with each other but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other. Now let’s write a logic for Implicit type casting to understand how it works.

public class Conversion{
public static void main(String[] args)
{
int i = 200;

//automatic type conversion
long l = i;

//automatic type conversion
float f = l;
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}

Output:

Int value 200
Long value 200
Float value 200.0

Now let’s move further and understand how Explicit Type Casting works.

Narrowing Casting

In this case, if you want to assign a value of larger data type to a smaller data type, you can perform Explicit type casting or narrowing. This is useful for incompatible data types where automatic conversion cannot be done.

Let’s understand this with the help of an example.

//Java program to illustrate explicit type conversion
public class Narrowing
{
public static void main(String[] args)
{
double d = 200.06;

//explicit type casting
long l = (long)d;

//explicit type casting
int i = (int)l;
System.out.println("Double Data type value "+d);

//fractional part lost
System.out.println("Long Data type value "+l);

//fractional part lost
System.out.println("Int Data type value "+i);
}
}

Output:

Double Data type value 200.06
Long Data type value 200
Int Data type value 200

Now that you know how to perform Explicit type casting, let’s move further and understand how explicit casting can be performed on Java expressions.

Explicit Type Casting in Expressions

When you are evaluating the expressions, the output is automatically updated to the larger data type of the operand. But if you store that result in any smaller data type it generates compile time error, due to which we need to type cast the output.

Example:

//Java program to illustrate type casting int to byte
public class ExplicitTest {
public static void main(String args[])
{
byte b = 70;

//type casting int to byte
b = (byte)(b * 2);
System.out.println(b);
}
}

Output:

140

Note:  In case of single operands the result gets converted to int and then it is type casted accordingly.

So that was all about Explicit Type Casting in Java. With this, we come to the end of this article. I hope you found it informative. If you wish to learn more, you can check out our other Java Blogs as well.

Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.

Got a question for us? Please mention it in the comments section of this “ Type Casting in Java” article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 14th December,2024

14th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Typecasting in Java and how does it work?

edureka.co