Thursday, January 12, 2017

Type Casting in Java

Type Casting:

----------------------------------------------------------------------------------------------------

The process of converting data from one data type to another data type is called as "Type Casting".

There are two types of Type casting in java:

1. Primitive data types type casting
2. User defined data types type casting

The process of converting data from one user defined data type to another user defined data type is called as user defined data types type casting.

To perform user defined data types type casting we have to provide either "intends" relation or "implementation" relation between two user defined
data types.

Primitive data types type casting:
------------------------------------------------

The process of data from one primitive data type to another primitive data type is called as "primitive data types type casting".

there are two types of primitive data types type casting:

a) Implicit type casting
b) Explicit type casting

a) Implicit type casting:
----------------------------
The process of converting data from lower data type to higher data type is called as implicit type casting.

To cover all the possibilites of implicit type casting java has provided the following chart:

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

In java application to perform implicit type casting, we have to assign lower data type variables to higher data type variable.

EX: byte b = 10;
    int i = b;

when we compile above code, when compiler encounter the above assignment statement, compiler will check whether right side variable data type is compatible with left side variable data type or not, if not, compiler will rise an error like "possible loss are precision" in jdk7. if right side variable
data type is compatible with left side then compiler will not rise any error and compiler will not perform any type casting.

When we execute the above code, when the JVM encounter the above assignment statement than JVM will perform the following two actions:

1. JVM will convert right side variable data type to left side data type [Type Casting].
2. JVM will copy right side variable value to left side variable.

EX:1.
      int i = 10;
      byte b = i;
      System.out.println(i+"  " + b);

Status: Compilation Error, possible loss of precision.

EX:2.
      byte b = 65;
      char c = b;
      System.out.println(b+"  " + c);
Status: Compilation Error, possible loss of precision.

EX:3. char c = 'A';
      short s = c;
      System.out.println(c+"  " + s);

Status: Compilation Error, possible loss of precision.

Reason: Conversion are not possible between byte and char, short and char in implicit type casting because their internal data representation are not compatible.

EX:4.
      byte b = 130;
      System.out.println(b);

Status: Compilation Error, possible loss of precision.

Reason: If the provided value is greater than the specified data type maximum value then compiler will treat that value is of the next higher data
type, in implicit type casting we are unable to assign higher data type value to lower data type variable.

EX:5.
     byte b1 = 60;
      byte b2 = 70;
      byte b = b1+ b2;
      System.out.println(b);
Status: Compilation Error, possible loss of precision.

EX:6.
      byte b1 = 30;
      byte b2 = 30;
      byte b = b1+ b2;
      System.out.println(b);
Status: Compilation Error, possible loss of precision.

Reason:
consider X,Y,Z are three primitive data types:
X + Y = Z

1. If X & Y belongs to {byte, short, int} then Z should be Int.
2. If either X or Y or both X & Y are belongs to {long, float, double} then Z should be higher (X,Y) as per implicit type casting chart.

EX:
byte + byte = int
byte + short = int
short + long = long
long + float = float
float + double = double
int + double = double
--------
--------


------------------------------------------------------------------------------------------------------------------------

2. Explicit Type Casting:
---------------------------------
The process of converting data type from higher data type to lower data type is called as Explicit type casting.

To perform explicit type casting, we have to use the following pattern:

P a = (Q) b;

where Q must be either same as P or lower than P.


EX:1.
      int i = 10;
      byte b = (byte) i;
      System.out.println(b+"  "+i);

Status: No compilation error.
OP: 10  10

When we compile the above program, when compiler encounter the above assignment statement, compiler is compatible with left side data type or not, if not, compiler will rise an error like "possible loss of precision". If right side data type is compatible with left side data type then compiler will not rise any error & compiler will not perform type casting.

When we execute the above program, the JVM encounter the above assignment statement then JVM will perform the following two actions:

1. JVM will convert right side variable data type to cast operator [data type] provided data type at right side explicitly.[Explicit type casting]

2. JVM will copy value from right side variable to left side variable.

EX:2.
       int i = 10;
      short s = (byte) i;
      System.out.println(i+"  " + s);

Status: No Compilation Error.
OP: 10  10

EX:3. 
      byte b = 65;
      char c = (char) b;
      System.out.println(b+"  " + c);

Status:No Compilation Error.
OP: 65  A

EX:4.
     char c = 'A';
      short s = (byte) c;
      System.out.println(c+"  " + s);

Status:No Compilation Error.

EX:5.
      short s = 65;
      char c = (byte) s;
      System.out.println(s+"  " + c);

Status: Compilation Error, possible loss of precision.

EX:6.
      byte b1 = 30;
      byte b3 = 30;
      byte b = (byte)b1 + b2;
      System.out.println(b);

Status: Compilation Error, possible loss of precision.

EX:7.
      byte b1 = 30;
      byte b3 = 30;
      byte b = (byte)(b1 + b2);
      System.out.println(b);

Status:No Compilation Error.

EX:8. 
      float f = 22.22f;
      long l = 10;
      long l1 = (long)f + l;
      System.out.println(l1);

Status: No compilation Error.

EX:9.
      double d = 22.22;
      byte b = (byte)(short)(int)(long)(float)d;
      System.out.println(b);

Status: No compilation Error.
---------------------------------
You may also like:--> Data Types in JAVA

No comments:

Post a Comment