Welcome to another tutorial on the "How to code in Java" tutorial series. In the last tutorial, we have gone through on what arrays are and how to use them. In this tutorial, we'll be going through on how type casting works in Java. Because there are many different types of data types in a programming language. Sometimes you will find yourself in a situation where the variables or values are not compatible with each other while assigning the values into a variable. That's when type-casting help us to solve the problem for us.
Index for the tutorial:
While using an IDE and by using Java, try declaring two variable (using the identifier/name that you likes), one with the type byte while the other one with the type char. They can be any value you want, but make sure they're in the respective size limit. Then, try adding the variable with the type byte into the variable with type char. If you're using an IDE and not TextPad, the IDE will show you an error for that particular line. This is because of the incompatible of a byte value been added into a byte variable. The variable "number_2" with the type byte has a bigger size compared to the char type, "number_1". This is when we need to change the data type of "number_2" to a compatible one.
Error is shown when the variable is not compatible with each other. |
You can fix this by converting the byte value into a char.
Fixing the error using explicit type- |
Type-casting means to change the data-type of a variable to another type. Don't worry much if now you don't understand how that works. I'll explain again later when that part comes.
There are many types of type-casting, but only two types are commonly used by programmers; implicit and explicit type-casting (conversion). The example that I showed you just now is an example of explicit type conversion. But let's go through with implicit conversion first.
Implicit Type-casting (Implicit conversion)
Implicit type casting or sometimes being called automatic conversion, happens automatically, without the need for the programmer to specify which data type to convert to. Well, this type of conversion only occurs under a few conditions.
- When those two data types are compatible. All numeric type is compatible with each other.
- When we assign value with a smaller data type into a bigger one. The target data type is larger than the value type (in the sense of size).
You might also have used implicit conversion before when you're playing around with Java. For example, by adding the two, with one being an integer while the other one being a float number. Although they can be added together, the output of the addition will depend on the way you store and display the result for the addition.
Let's say you straight up print out the result to the terminal, Java would display the addition result as a floating number (with the type double). This is because in Java when there's a floating number in an expression (like adding, subtract, division, multiplication and many more), the whole expression will be treated as a floating number. This also the same when you store the result into a variable with type double.
Directly display the result of the expression into the terminal. |
Storing the value into a variable with type double. Then display the result. |
The two of the code produced the same result. |
Java will also automatically promote byte, short, and char operands to an int type when evaluating an expression.
But this will be different when you store the result in an integer variable. If you're using an IDE, then the IDE will let you know there's an error before you go and start compiling your code. The error is caused by the incompatible of a double value stored into an integer type variable. As the size of a double type is bigger compared to an int data type as well as different from each other in the form of structure. To fix the error, we can just use explicit conversion to change the double's value into an integer value. This changed the whole result to be in integer form, ignoring the fraction number.
Changing the code to an output integer value. |
Result for the code above. |
That's also the case for the example just now, where the value stored inside a byte type variable. I used type casting to change the "number_2" variable's value into a char type. Giving the value compatible to be assigned into "number_1".
Explicit Type-casting (Explicit conversion)
Explicit conversion will need the programmer to specify which data type you want the variable to change to. This allows the programmers to convert the incompatible data types into a compatible one. How do we change that in Java? Well, we just need to select the data-type near the left of the variable or value that we want to change type. While surrounding the type with open and close parentheses. Some example would be the two example just now. Since they cannot be converted automatically by Java, we need to convert them ourself to another compatible type.
Another thing is that even if the variables data type is compatible with each other. You can always use type casting to change their value into your desired form. For example, you want to change every value returned from a method to be in int type, no matter if the calculated value produced a floating number.
Limitation of type-casting
Well, there's also one thing that numeric type cannot be converted to and that's boolean to avoid programmers to accidentally caused an error in their program. Read more about in this Stack Overflow thread.
Well, that's all I could explain about type-casting in Java. I hope you now know how type-casting works and when to use them. If you find this tutorial helpful, please share it with others that might find it helpful. As always, if you have any questions, critics or suggestions, feel free to post a comment in the comment section below. For the next tutorial, we'll be going through what are methods in Java.
No comments:
Post a Comment