# What is Casting Process in Java?

\$\$/\$\$

Let us now move on to a very important concept in programming called “Casting”. This is a process that will let you know how you can convert one data type to another. It is helpful in many situations and will be of immense use to you when you write programs. Let’s see how you can go about it.

\$\$/\$\$

## Video Transcript

Recall the scenario when we had three different integers which were storing the marks of three different students. That is, I have an integer x 20, an integer y 40 and another integer 50. Now, remember that we were trying to calculate the average of these marks and we concluded that the average of these mathematically comes out to be 36.6. But if I try to calculate the average and store it in double, or if I try to store it in int, it does not matter. And this 36.6 always gets stored as 36. Now, suppose one way to resolve this situation was to initially itself convert these INTs into double. That is, if I would have stored 2040 and 50 as 20.0, 40.0 and 50.0, then it would have been possible for me to calculate the average as 36.6. But suppose that in the beginning, I do not know whether my end result is going to be a whole number or a number with decimal. So in that case, is there a way to resolve this situation? Well, recall that if I say double average is equal to x plus y plus z divided by three, then it gave me the result 36.0 instead of 36.6. So this basically gives me an incorrect result. Suppose that if I wish to treat these integers as double itself for performing a single line operation, for example, I need to calculate the average and only for this particular statement I wish that the computer would treat these integers as double values. Then is there a way to do it without actually changing the data type of these particular variables? Well, yes, we can tell the computer that temporarily for only this particular statement, please treat x, y and z not as integers but as double values. So whenever we ask the computer to treat one form of data type as another form only temporarily, this process is known as casting. So whenever you wish to cast an integer as a double, you enclose the word double inside the bracket just before it. So I would say that double average is equal to I. Then tell the computer that treat x, y and z as double and not as integer, even though they actually are integers. So I would say the double average is equal to double of x. That is, x should be cast as double plus double of y. That is, y should be treated as a double plus double of z. That is, z should also be treated as a double and divided by three. So in this case, what happens that even though x, y and z are originally integers, but when I put this double keyword in the brackets just before x, y and z. So this instructs the computer to temporarily treat x, y and z as double values. So in this case x, y and z, in this statement they get temporarily converted into 20.0 plus 40.0 plus 50.0 divided by three. Notice that I have not changed the data type of x, y and z. That is x, y and z are still integers, but only for this statement they are getting treated as double values. So in this case, 20.0, 40.0 and 50.0 addition gives me one, one 0.0 divided by three, which gives me 36.6. So here I have been able to obtain my correct value successfully. So in this way, whenever you encounter such situations where your original data type might be something else, but your final data type might be something else and you still need to perform arithmetic operations on them. So only for some particular statements, you can treat those integers as double values and still be able to perform these calculations. So this process of treating one data type temporarily as another kind of data type is known as casting in Java. So in this way I prevented the situation where initially I had to declare them as double. That is, initially these were three integer values and still I could perform arithmetic operations on them and retain a large amount of accuracy. So this is how casting works. So now you have seen and played around a little with arithmetic operators and casting. So let us try to solve these problems and see if you can guess the correct output. So here what I do. I take int x equals to three, y equals to two, and the result of x by y, I'm storing it into AZZ. So this is a case of without casting. So remember, in cases where there is no casting, division of two integers always gives me an integer. So x by y is going to give me three divided by two, which is one. So in this case, z will be equal to 1.0. But take a look at this case here again, x is three, y is two and z is stored as double of x and double divided by double of y, that is x and y are temporarily here treated as 3.0 divided by 2.0. So in this case, z will contain the value 3.0 divided by 2.0, which is 1.5. So here you can see how casting is actually useful when performing division and for converting and temporarily treating an integer data type as a double data type.

## Video Recap

• The segment explains the scenario of calculating the average of three integers

• The average mathematically comes out to be 36.6, but when stored in double or int, it gets stored as 36

• Casting can be used to temporarily treat integers as double values for specific statements without changing their data type

• To cast an integer as a double, enclose the word double in brackets just before it

• Casting can help in situations where the original data type is different from the final data type and arithmetic operations need to be performed

• The segment demonstrates casting in Java and shows how it can be useful in performing division and temporarily treating integers as double values

• Casting helps in retaining a large amount of accuracy while performing arithmetic operations on different data types

• The segment provides examples of performing division with and without casting and how the results differ.

So you converted an int to a double and a double to an int as well. By now, you must have an idea of the kind of situations where this can be useful to you. Just to recap, in case you want to convert an int variable ‘x’ to a double, you write it like this:

(double) x.

And now, you can store this in another double variable, say, ‘y’. So, double y = (double) x; will convert an int ‘x’ to a double and store the value in ‘y’.

Please note that here x is converted to a double only for the statement double y = (double) x; . The variable “x” remains of the type int only; i.e. if you use x somewhere else, it will behave as an int. Hence this is just a temporary conversion to a double.

\$\$/\$\$

As we all already know, computers store everything as numbers or as a series of 0s and 1s. Hence, when you try to store a char value, it will be stored as a number in the computer’s memory with an indicator flash that this value is to be interpreted as a character represented by that particular number. Let us learn more about this through the following video.

\$\$/\$\$

Open your IDE and try running the following code in the main class and observe the output:

```a='A';
int b;
b= (int) a;
System.out.print(b);
```

You must be able to observe the output as:

65

Here, “65” represents the character “A”. Such representations are standardized by ASCII(American Standard Code for Information Interchange).

You can go here and take a look at the table which has ASCII values for all characters available.

\$\$/\$\$

Let us now see casting in action.

You can download the following java file which will be very useful to you while watching the video. Right click on the java file and open it with Intellij. Please be careful with the name of the java file. The name of the java file should exactly be same as the class name inside it for the code to run. So make changes accordingly.