Type Casting in Java

 

We deal with different data types in Java. Such as int, long, double, char, etc.

These data types have their own size.

For example, double data type requires greater space in the memory compared to float. Like that ‘byte’ requires lesser space compared to ‘short’ data type.

But, every data type occupies certain amount of space in the memory.

We always try to assign the exact data type for a specific type of data. For integer value we can assign ‘int’ or ‘long’ data type, for character we assign ‘char’ data type.

Now, when we assign the value of one data type to another data type is known as type casting.

 

There are two types of type casting:

1.     Automatic type casting (Widening)

2.     Manual type casting (Narrowing)

 

Automatic type casting:

Automatic type casting is the process of assigning larger data type to a variable from a smaller data type.

Example:  byte -> short -> char -> int -> long -> float -> double.

It is not necessary to be in the same sequence shown above.

It can be from ‘byte’ to ‘char’ or maybe from ‘int’ to ‘double’.

Note: Boolean data type cannot be type casted to any other data type.

As the name suggests automatic type casting is performed by the machine automatically and does not require and manual instructions to be given to the compiler.

Java Programming for Automatic Type Casting:

package typeCasting;

 

public class TypeCasting {

 

     public static void main(String[] args) {

 

         

          byte x = 2;       

          int y = x;        

          float f = y;      

          double z = f;

         

         

          System.out.println(x);

          System.out.println(y);

          System.out.println(f);

          System.out.println(z);

         

 

     }

 

}

 

Console Result:

2

2

2.0

2.0

 

 

 

Manual type casting:

Manual type casting is just opposite phenomenon of automatic type casting.

Manual type casting is the process of assigning smaller data type to a variable from a larger data type.

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

Same above sequence need not to be followed but you can also type cast from double to long or may be char to byte

In this case of type casting you have to instruct the compiler through a small piece of coding to accomplish the task.

Compiler cannot do narrowing automatically.

 

Java Programming for Manual Type Casting:

package typeCasting;

 

public class TypeCasting {

 

     public static void main(String[] args) {

 

          double x = 24.7568;

          int y = (int)x;

         

          System.out.println(x);

          System.out.println(y);

         

 

     }

 

}

 

Console result:

24.7568

24

 

As you can see, when we perform the above type casting from double to int, it always prints the floor value and not the ceiling value.

 

 

Type Casting in Java Type Casting in Java Reviewed by Technobits on February 24, 2021 Rating: 5

No comments:

Powered by Blogger.