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

Type Casting in Java

Java Type Casting

Uploaded by

amanvaswani14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Type Casting in Java

Java Type Casting

Uploaded by

amanvaswani14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Type Casting in Java

In Java, type casting is a method or process that converts a data type into another data
type in both ways manually and automatically. The automatic conversion is done by the
compiler and manual conversion performed by the programmer. In this section,

Type casting
Convert a value from one data type to another data type is known as type casting.

Types of Type Casting


There are two types of type casting:

o Widening Type Casting


o Narrowing Type Casting

Widening Type Casting


Converting a lower data type into a higher one is called widening type casting. It is also
known as implicit conversion or casting down. It is done automatically. It is safe
because there is no chance to lose data. It takes place when:

o Both data types must be compatible with each other.


o The target type must be larger than the source type.

1. byte -> short -> char -> int -> long -> float -> double
For example, the conversion between numeric data type to char or Boolean is not done
automatically. Also, the char and Boolean data types are not compatible with each other.
Let's see an example.

WideningTypeCastingExample.java

1. public class WideningTypeCastingExample


2. {
3. public static void main(String[] args)
4. {
5. int x = 7;
6. //automatically converts the integer type into long type
7. long y = x;
8. //automatically converts the long type into float type
9. float z = y;
10. System.out.println("Before conversion, int value "+x);
11. System.out.println("After conversion, long value "+y);
12. System.out.println("After conversion, float value "+z);
13. }
14. }

Output

Before conversion, the value is: 7


After conversion, the long value is: 7
After conversion, the float value is: 7.0

In the above example, we have taken a variable x and converted it into a long type. After
that, the long type is converted into the float type.

Narrowing Type Casting


Converting a higher data type into a lower one is called narrowing type casting. It is
also known as explicit conversion or casting up. It is done manually by the
programmer. If we do not perform casting then the compiler reports a compile-time
error.

1. double -> float -> long -> int -> char -> short -> byte
Let's see an example of narrowing type casting.

In the following example, we have performed the narrowing type casting two times.
First, we have converted the double type into long data type after that long data type is
converted into int type.

NarrowingTypeCastingExample.java

1. public class NarrowingTypeCastingExample


2. {
3. public static void main(String args[])
4. {
5. double d = 166.66;
6. //converting double data type into long data type
7. long l = (long)d;
8. //converting long data type into int data type
9. int i = (int)l;
10. System.out.println("Before conversion: "+d);
11. //fractional part lost
12. System.out.println("After conversion into long type: "+l);
13. //fractional part lost
14. System.out.println("After conversion into int type: "+i);
15. }
16. }

Output

Before conversion: 166.66


After conversion into long type: 166
After conversion into int type: 166

You might also like