Type Casting In Java

Data type casting in java

Well, data type casting refers to converting the data from one type to some other type(well, this is of course if the data is convertible). There are two types of type casting available. One is implicit(widening), in which we have to do nothing, and then another is explicit(manual), in which we do the typecasting. We are going to discuss both types. In other words, the typecasting may be required to be done, or sometimes is done without even us being familiar.

Let’s consider some examples so that we get a clear picture of why we even need to bother about the typecasting. Consider that there is some box, a big box, which can store something. Now, if you have something that does not fit into the box, and also you have to fit that thing into that same box, what would you think of is adjusting somehow to fit that particular thing into the box?

This resembles quite the explicit typecasting, in which we are required to adjust that value somehow so that it fits into that particular variable. For a formal example, let’s consider the below example –

public class HelloWorld {
public static void main(String[] args){
double floatingNum = 123.45;
long convertedNum = floatingNum;
}
}

The underlined and bold line in the above program produces an error, stating that it requires a long number but it got a double type data. So, to overcome this error, we need to explicitly change the type of that data from double to long. This is pretty much simple. Here is how we explicitly do the typecasting. When we need to convert some data from one type to another, we write the required type(long in our case here) before the data, into brackets. Have a look at our example below –

public class HelloWorld {
public static void main(String[] args){
double floatingNum = 123.45;
long convertedNum = (long) floatingNum;
}
}

Now, the error has been resolved, and everyone’s happy! You can try printing the value in converted nun as well.

This is how we are doing explicit typecasting. I hope you have understood what are we willing to do. We are willing to fit in some value which actually does not fit into that container. So in such a case, we need to convert that value to some value that can be stored there. Remember that we are explicitly adjusting the value to fit into the container, which is why there might be a loss of data. Like in the above example, we had a value of 123.45, but when we converted it too long, it became 123. So, there is quite data loss.

There is another type which is called implicit typecasting. This is like a one-liter bottle that can hold 500ml of water too. So, if we need only 500ml of water, we need not just go with a 500ml bottle, but can also use that 1-liter bottle.

This is done implicitly so we do not have to take care of this thing. One example of this can be seen as follows-

public class HelloWorld {
public static void main(String[] args){
long convertedNum = 9876;
}
}

Well, the thing is that the literal (9876), is actually an integer, being stored in a long type variable. So it is being implicitly converted to long.

So, this was quite about typecasting. We should be aware of this thing since we might sometimes require to do the typecasting, or sometimes it happens implicitly, so we should be aware of that too.