Data Type Casting (Type Conversion)

Java supports two types of castings – primitive data type casting and reference type casting. Reference type casting is nothing but assigning one Java object to another object. It comes with very strict rules and is explained clearly in Object Casting. Now let us go for data type casting.

Java data type casting comes with 3 flavors.

  1. Implicit casting
  2. Explicit casting
  3. Boolean casting.

1. Implicit casting (widening conversion)

A data type of lower size (occupying less memory) is assigned to a data type of higher size. This is done implicitly by the JVM. The lower size is widened to higher size. This is also named as automatic type conversion.


In the above code 4 bytes integer value is assigned to 8 bytes double value.

2. Explicit casting (narrowing conversion)

A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed by the programmer. The higher size is narrowed to lower size.

In the above code, 8 bytes double value is narrowed to 4 bytes int value. It raises error. Let us explicitly type cast it.

The double x is explicitly converted to int y. The thumb rule is, on both sides, the same data type should exist.

3. Boolean casting

A boolean value cannot be assigned to any other data type. Except boolean, all the remaining 7 data types can be assigned to one another either implicitly or explicitly; but boolean cannot. We say, boolean is incompatible for conversion. Maximum we can assign a boolean value to another boolean.

Following raises error.

byte –> short –> int –> long –> float –> double

In the above statement, left to right can be assigned implicitly and right to left requires explicit casting. That is, byte can be assigned to short implicitly but short to byte requires explicit casting.

Following char operations are possible.

Object casting (reference casting) discusses casting between objects of different classes involved in inheritance.


Your one stop destination for all data type conversions

byte TO short int long float double char boolean
short TO byte int long float double char boolean
int TO byte short long float double char boolean
float TO byte short int long double char boolean
double TO byte short int long float char boolean
char TO byte short int long float double boolean
boolean TO byte short int long float double char

String and data type conversions

String TO byte short int long float double char boolean
byte short int long float double char boolean TO String

Some questions for you from java.lang package.

1. How many ways you can copy one object properties to another?
Ans: 3 ways – Shallow copying, Deep copying and Cloning.
2. How many types of inner classes exist?
Ans: 4 types.
3. What are JAR files?
Ans: JAR file is a zipped file zipped by JVM.
4. How to convert a string into string form?
Ans: String to data type conversions – byte, short, int, long, float, double, char and boolean.
5. How to convert an object to string?
Ans: Object to String – toString()
6. Do you know the functionality and methods of following java.lang package classes?
Ans: 1. Root class – class Object 2. class Character 3. class System
7. What are wrapper classes?
Ans: Wrapper classes
8. What is the garbage collection mechanism in Java?
Ans: Garbage Collection – gc() and exit(0)
9. How to use the C++ destructor functionality where Java does not support destructors?
Ans: Java Destructor – finalize()
10. How to compare two objects?
Ans: Object comparison – hashCode() & equals()

17 Responses

  1. Amar July 30, 2015 / 11:49 pm

    Suppose if i give inputs from keyboard to a variable and if i declare that variable as int and the given input is in double type(eg.2.3) and i have declared int for that variable so i get exceptions pls guide me
    import java.util.Scanner;
    class Program
    public static void main(String[] args)

    Scanner s1=new Scanner(;
    int x1=s1.nextInt();// if i give here input as 12.3 i get error
    System.out.println(“x1 value : “+x1);


  2. akash agrawal June 23, 2014 / 11:27 am

    Sir as per your notes from small data type to large data type it is implicit conversion and for high data type to small data type we reqiure explicit conversion so how a long which is a data type converted automatically into float by jvm automatically as per your type promotion chart.

    Pls reply important

    • S. Nageswara Rao, Corporate Trainer June 23, 2014 / 12:55 pm

      See my notes given:

      byte –> short –> int –> long –> float –> double

      The left-side value can be assigned to any right-side value and is done implicitly. The reverse like float to short requires explicit casting.

      I put another question to you. double and long each take 8 bytes memory. Which can be assigned to what? long can be assigned to double but double cannot be assigned to long. Why? It is due to floating-point acceptance.

    • akash June 23, 2014 / 1:02 pm

      Sir what is this floating point acceptance could you pls explain.

      • S. Nageswara Rao, Corporate Trainer June 23, 2014 / 1:38 pm

        floating-point matters in the designing of data types conversions. For this reason, long can be implicitly converted to double but double cannot be implicitly into long.

  3. parth January 2, 2014 / 9:17 am

    hi frnds, i wanna to learn Java , how can i do this in the simple way.

  4. kesav September 26, 2013 / 1:07 pm

    sir, is type conversion and type casting the same in java?

  5. nithin September 11, 2013 / 11:13 am

    Write a program to print the largest value of byte, short, int, long, float and double data types. Output should be separated by space.

    • S. Nageswara Rao, Corporate Trainer September 11, 2013 / 11:43 am

      Do not ask anybody to write a program give to you. Ask anyone some tips how to solve the problem. For your problem use the methods like

      public static final float MAX_VALUE;

      Life every wrapper class has MAX_VALUE, check and find in Java API.

  6. ujjwal January 1, 2013 / 2:45 am

    Can a byte value be cast to a double ?

  7. Deep September 23, 2011 / 9:31 am

    sir,Is there any benifit of implicit casting.

    According to good coding standard,Which is better to use composition or inheritence in?

    • S. Nageswara Rao, Sr.Faculty September 25, 2011 / 2:24 pm

      Without object castings no Java coding at all. You use casting operations in collections classes (DS), RMI and in many places. Reg. composition and inheritance, everyone has got their own applications in realtime coding.

Leave a Reply

Your email address will not be published. Required fields are marked *