Day4 : Type Conversion and its type

 Type Conversion

Type conversion is a concept which is applied by java compiler whenever we use Assignment operator between variable of different datatypes.

Types of Type conversion

There are two types of type conversion :
  1. Implicit Conversion ((1) Compatible (2) Range of RHS < Range of LHS) (Done by Compiler)
  2. Explicit Conversion (In Java) But In C language called as Type Casting. (To be done by the programmer)

Rules of Implicit Conversion:

1) Values must be compatible i.e.., there must be some way in java to convert RHS to LHS.
    For Example: int x;
                           x = 'A';
The above code will Run because java will convert character constant 'A' to its UNICODE equivalent 65 because character is compatible with integer.
But,
            int x;
            x = true;
//The above code will not even compile because the java compiler has no way of converting boolean to int. so the error will incompatible type: can't convert boolean to int.

2) The value on RHS of assignment operator must be smaller in terms of Range as compared to the variable on LHS.
For Example  : 
        double x;
        x = 10;
The above code will compile and Run because 10 is an integer which is a smaller type as compared to the data type of x which is a double.
But,
        int x;
        x = 10.5;
The above code will not even compile because 10.5 is double which is of higher type as to the datatype of x which is int and the error message will be possible lossy conversion from double to int.

If we want the above code to run then we will have to use explicit conversion as shown below:
                        int x;
                        x = (int)10.5;
Now, the code will compile & run but x will store 10.
When the range is Overflow, then any language which uses range conversion does rotate(overflow).

1) Integer :

Case-1:
            byte a = 10;
            int n;
            n = a;
In the case 1, 10 is assigned in a, and a's datatype is byte and the range of byte is -128 to 127. As we know that Range of int datatype is larger than byte. So, the code will easily compile and run.

Case-2:
            int a = 10;
            byte n;
            n = a;
In this case, Code will not compile because you're going from int to byte. It will throw an error Lossy conversion from int to byte.




If you want to go the above code to Run then you follow with explicit conversion.
             int a = 10;
             byte n;
             n = (byte)a;
Now, your program is ready to compile.



Case-3:
             int n = 128;
             byte a;
             a = n;
As we know that Range of byte is -128 to +127 so the above code will not compile because you're going against the Rule of Implicit Conversion i.e., Range of int is less than Range of Byte.
If you use Explicit conversion of above code, it will run and the output as -128.

2) Short

Case-1:
            short s = 10;
            int n;
            n = s;
Range of Short is : -32768 to 32767


Case -2:
            int n = 10;
            short s;
            s = n;
Error: Lossy Conversion from int to short.
Explicit Conversion : 
            int n = 10;
            short s;
            s = (short)n;
Output : 10

Case - 3:
            int n = 32768;
            short s;
            s = n;
Error: Possible lossy conversion from int to short

Explicit Conversion:
            int n = 32768;
            short s;
            s = (short)n;

3) Long

Case1:
        int a = 10;
        long b;
        b = a;
Output : 10

Case2:
        long b = 10;
        int a;
        a = b;
Error : Possible lossy conversion from long to int.
Explicit Conversion:
        long b = 10;
        int a;
        a = (int)b;

Case3:
        long a = 2147483648;
        int b;
        b = a;
Error: integer number is too large Or number too large to be represented as integer.



Case4:
        long a = 2147483648L;
        int b;
        b = a;
Error: Possible Lossy Conversion from long to int.
Explicit Conversion:
        long a = 2147483648L;
        int b;
        b = (int)a;
Output : - 2147483648
// L or l --> Suffix declaration(Java can understand both capital L and small l)


Practice Question - 
a)int n;
   n = 2147483648;
Error : Too large

b)int n;
   n = (int)2147483648;
Error: Too large

c) int n;
    n = 2147483648;
 Error: Possible lossy conversion;

d) int n;
    n = (int)2147483648;
Output: -2147483648

e) float n;
    n = 2147;
Error: Too Large

f) float n;
    n = 2147483648L;
Successfully Compile and Run

g) What you called?
    (i) 'A' -- Char constant
    (ii) "A" -- String Constant
    (iii) 25 -- Integer Constant
    (iv) 2.5 -- Double Constant

4) Double

Case1: 
        double a = 1.7;
        float b;
        b = a;
Error: Possible Lossy Conversion from double to float.
Explicit Conversion:
        double a = 1.7;
        float b;
        b = (float)a;
Output : 1.7



Case2 : 
        float b = 1.7;
        double a;
        a = b;
Error : Lossy Conversion from double to float.


The above code will throw error because we've not use suffix declaration of float value. The correct program is shown below:

        float b = 1.7f;
        double a;
        a = b;

Output : 1.7

//Note : Expression of any operation which has operator and operand is called expression.

5) Char 

Case1 : 
            byte a = 10;
            char ch;
            ch = a;
Error : Possible Lossy Conversion from byte to char.

Case2 : 
            char ch = 'A';
            byte b;
            b = ch;
Error: Possible Lossy conversion from char to byte.

Case3 : 
        short a = 10;
        char ch;
        ch = a;
Error: Possible Lossy Conversion from short to char.

Case4 : 
        char ch = 'A';
        short s;
        s = ch;
Error : Possible lossy conversion from char to short.

Case5: 
        int a = 10;
        char ch;
        ch = a;
Error: Possible lossy conversion from int to char.

Case6:
        char ch = 'A';
        int n;
        n = ch;
This code is successfully run because in the UNICODE the value of 'A' is 65 that's why it will easily assign in integer(i.e.., char to int).

6) Boolean

Case1 : 
        int x = 1;
        boolean y;
        y = x;
Error: Incompatible Type

Case2:
        boolean y = true;
        int x;
        x = y;
Error : Incompatible type

Case3:
        int x = 1;
        boolean y;
        y = (boolean)x;
Error: Incompatible type

Case4:
        boolean y =true;
        int x;
        x = (int)y;
Error : Incompatible type


  • double , float ---> Double
  • long , int ---> float
  • char , short , byte ---> int

        


Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?