What is Typecasting in Java and How does it work?

Swatee Chand
Edureka
Published in
5 min readJul 25, 2019

--

Programming is playing around with data. In Java, there are many data types. Most of the time 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:

  • What is Type Casting?
  • Widening Casting
  • Narrowing Casting

Let’s get started!

What is Type Casting?

Typecasting 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 cast 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 typecasting.

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 the 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 a compile-time error, due to which we need to typecast 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 typecast 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 as well.

With this, we come to an end to this blog. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Java.

1. Object Oriented Programming

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java Tutorial

6. Java Array

7. Java Collections

8. Java Threads

9. Introduction to Java Servlets

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Advanced Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Comparable in Java

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Nested Class in Java

34. Java Collections Interview Questions and Answers

35. How to Handle Deadlock in Java?

36. Top 50 Java Collections Interview Questions You Need to Know

37. What is the concept of String Pool in Java?

38. What is the difference between C, C++, and Java?

39. Palindrome in Java- How to check a number or string?

40. Top MVC Interview Questions and Answers You Need to Know

41. Top 10 Applications of Java Programming Language

42. Deadlock in Java

43. Square and Square Root in Java

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on July 25, 2019.

--

--