Skip to content
APPCITORThe Tech Blog
Esc

Part 8 – Conversion and Casting

July 24, 2020 1 min read

Part 8 – Conversion and Casting

Hi, Today’s article is very important because it is not easy to understand. And previous articles also important for this topic Conversion and casting. But don’t confuse guys you will definitely understand the lesson by the future articles also clearly. But I’m trying to explain you at my best.

So let’s go to the today’s topic.

Conversion

Look at the code given below.

Here the process that happened in the code is a byte value is converted to an int value. It’s a automatic conversion.

See the output.

No. of bits of a byte is 8 and no.of bits of an int is 32. So a byte value is smaller than an int value. But a char value cannot be converted in to a byte value or a short value. Because char is a 16 bit value.

Casting

Let’s go with an example.

Here the int value is converting to a byte value. But an int is larger than a byte. Therefore an auto casting is not happening here. So we should do the casting to the code to be compiled. For that we used to write byte in side the brackets as shown in the program above, because we use last 8 bits of int for casting.

See the output.

Below given is a image for the better understand.

I hope you all understand about conversion and casting.

Let’s meet with the next article on the topic Java flow control “if”, “if\else” and “if/else if”.

Thank you.

Share this article

Keep reading