Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Type Casting


So far we have seen, how to add two numbers that are of the same type.


i.e. We have added two numbers that are of Data Type int.


Example :



public class MyApplication
{
    public static void main(String[] args)
    {
        int x = 5;
	    int y = 6;
	    int z = x + y;
   		System.out.println(z);
    }
} 


Output :



  The added value is : 11

So, if we see the above output, we got the correct added result (i.e. 11) because the Data Type of x and y is int because both have an integer value in it.


int x = 5;
int y = 6;

Similarly, if we see the the variable z, it is also int. Because z holds the added value of the variables x and y. Which is also an integer(i.e. 11).


Now, there might also be a scenario where you want to add a floating point number and a whole number. And get the result as a whole number.


i.e. We want to add the whole number 5 and a floating point number 6.5. And the output we want should be 11 and not 11.5.


Let us see in the below example.


Example :



public class MyApplication
{
    public static void main(String[] args)
    {
        int x = 5;
	    float y = 6.5F;
	    int z = x + y;
        System.out.println("The added value is : "+z);
    }
}


Output :



  java: incompatible types: possible lossy conversion from float to int

Well! If we see the output! It says, incompatible types: possible lossy conversion from float to int.


Which in simple words means, a floating point number (i.e. 6.5) and and a whole number (i.e. 5) when added cannot produce an integer.


Now, if we see the Data Types of all the variables :


For variable x, it is int.For variable y, it is float.For variable z, it is int.


Which says, z cannot hold an integer.


So, how do we solve this problem?


And this is where, Type Casting comes into picture.


Let us rewrite the above program with Type Casting.


Converting a Float Data Type to an int Data Type


A solution to it would be, to convert the added value to int.


Example :



public class MyApplication
{
    public static void main(String[] args)
    {
        int x = 5;
	    float y = 6.5F;
	    int z = (int)(x + y);
        System.out.println("The added value is : "+z);
    }
}	 


Output :



  The added value is : 11

Now, if we see the output,


The added value is : 11

We can see the added value of 5 and 6.5 is 11 and it didn't result an error.


And how did we achieve this?


Well! By Type Casting.


Now, if we see the above code, we have assigned the number 5 to a variable x.


int x = 5;

And the floating point number 6.5 to variable y.


float y = 6.5F;

Now, we don't want the added result as 11.5. But we want only 11.


We have used type converter to convert the added value to int.


int z = (int)(x + y);

We have simply used int before the added value x+y

Spring_Boot

And now since the added value is converted to int Data Type, there is no problem. We get the output as 11.


The same rule applies to convert any other data types.