- Podcasts
- The Java Developers Hub
- Java Type Casting Explained | Type Conversion & Type Promotion for Beginners
Java Type Casting Explained | Type Conversion & Type Promotion for Beginners
The Java Developers Hub
- December 3, 2026
- 37 min
- Episode 2
About this episodeUnderstanding data types and how they interact is an essential part of learning Java programming. Java provides different mechanisms for…
- Understanding data types in Java.
- What type conversion means in Java.
- Understanding implicit (widening) type conversion.
- Understanding explicit (narrowing) type casting.
- Differences between widening and narrowing conversions.
- How to convert between numeric data types.
- Understanding type promotion in arithmetic expressions.
- How Java handles operations involving different data types.
- Common type casting errors and data loss.
- Practical examples of Java type conversions.
- Best practices for handling data types in Java.
- Beginners learning Java programming.
- Computer science and software engineering students.
- Developers transitioning to Java.
- Anyone interested in understanding Java data types.
- Students preparing for Java programming tests and technical interviews.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Type Casting Explained | Type Conversion & Type Promotion for Beginners
Welcome back Coder Army to our Java series and today we are going to talk about a very small but very interesting topic which is Type Conversion in Java. So before talking about this topic, I want to tell you that we make videos with a lot of motivation but when you don't support us with that motivation, don't like us, don't comment on us, then it doesn't feel good. So, from the first video till now, I will suggest you that whenever you are watching a video, like it, comment on it, ask us questions, if you have any opinion, if you enjoyed reading something, if you didn't enjoy reading something, mention that too. And please share whatever you are reading on LinkedIn and Twitter. Until you share your progress, You won't be able to enjoy reading it. By sharing progress, you get learning. You see that what you have learned, you can share it with the society. And secondly, you build a community around it. So whenever you learn something and share it, people are watching it. So your community is growing. And you can promise yourself that I will watch a video every day, I will share my content every day. So that I also have a record of what I have learned. If an interviewer is checking your profile tomorrow, he will also know that he has learned so many things and he also shares them every day. So do all this work. Be actively involved in watching the video. Not just passively watch the video and skip it.
You know, we had talked about it. data types in java now which data types come in data types first of all we get integers in integers you also knew that there is byte, short int, long etc etc and in the same way we get real numbers real numbers and we get characters and we get booleans right, we had read all this These were all our data types. In this, you had float and double. Right? In this, you had care. And in this, your name was Boolean.
So, before that, we have to understand that What are the rules of type conversion? So Let's start from the top Type Conversion in Java We have to see this thing First of all There are two types of type conversion in Java Two types One is your implicit And one is your explicit Now, what does it mean by implicit and explicit? Look, the type of conversion that Java does on its own is called implicit. And the type of conversion that Java does on its own, we have to tell it what to do, we.
And I also declared an int variable, just declared, not defined, int i, right? Now suppose I want to store the value of this byte in this i variable. So if I write something like this in my code, i equal to b. So will it work? So will my byte value b enter in i quietly? This means if I try to print this in the next line. So if I write system.out.println and I mention i. So, will my 24 print here also? The answer is yes, it will. Because this comes in implicit type conversion. So, why is this happening? How will this be converted in the code? So, let's see this in rules for implicit cast. So, there is an important rule for implicit casting. And what does this rule say? It will clear everything. Implicit conversion, sorry. Implicit conversion. And this rule says that that your destination data type in which you are moving, destination data type should be wider than your source data type.
Wider than your source data type. What does this line mean? Look, understand this carefully. What we did above, if you look carefully in our code, we tried to assign a byte value to an integer and it worked easily. We wrote i equal to b and this statement worked. Now I will show you in the code that it will work easily. Why did this happen? Because this line was satisfying. Our destination, which was the integer, It is a wider data type than our source, i.e. our byte. You must have understood what wider means. Our byte, do you know what is the range of byte? The range of byte is from minus 128 to plus 127. This is the range of byte. And how many bits does it have?
Because the range of integer will be in that range, sorry. Because the range of integer will be from negative something to plus something. In that range, this will also come. From minus 128 to plus 127. So whatever I can express in byte, I can also do that in int. So this is called implicit conversion. In this, Java will automatically convert this. There will be no problem. So if we write this simple line.
So, this type of conversion is called widening conversion. What do we call it? Widening conversion. Because this conversion happens automatically and in wider data type. So our destination should be wider than source. Now you must have understood that same is applicable if we let's say try to convert short to int or try to convert int to long. Everything should be the same. And it happens. We call this type of conversion widening conversion. So, after this, what is our second type of conversion? If we read the second, then the second type of conversion is your explicit conversion. Now, what is this? Now, what does explicit conversion say? Explicit says that your destination will be smaller than the source this time. So, Java cannot convert it by itself. So, if it does not convert by itself.
As soon as I write this line, I will get a red line and when I go to that red line, I will get to know the reason that I cannot convert integer to byte Now you must have understood the reason, if I give the same reason that our integer is 32 bit and our byte is only 8 bit Now in this 32-bit, a big number can come. Now when I convert this to a small number, then my data will not be truncated, the data will not be cut. So Java will not do this on its own. Java will say, if you want to do it, then you can use casting. I will not do it on my own. So this line will be wrong. Okay, this line will be wrong. If you have to do it, if you still want to do it, then you can write the exact line, syntax-wise, something like this.
This line is absolutely correct. In this line, you used casting. You said that I have to convert i to b. So whatever value of i comes, I am casting it in byte. I am telling the compiler that I want to cast this i in b. So I told the compiler that I want to do this. I am doing this intentionally. So what keyword do I have to write for it? I have to write in bracket in which I am casting. The name of that datatype is byte. and the one I want to cast. So, b is equal to byte i. If I write it like this, then my casting will be done. But the question is, now if I write, let's say, let's write it in white, now if I write system, The answer is very simple. If you go to the first thought principle and think that what was the value of my integer i? It was 300. So what will happen? When I write int i equal to 300 This 300 will be saved in memory And what will happen to save in memory?
2 4s are 8, which means 1 2 2s are 4, which means 0 and 2 1s are 2, which means 0 So our binary will be 1 double 0 1 0 double 1 double 0 So this is our binary So we have represented 300 like this Now look We know what is the range of an int The range of an int is 32 bits So how many are these? 1, 2, 3, 4, 5, 6, 7, 8, till here 8 has come.
Now if till here 8 has come, then we will make all other bits 0. So here 0, 0, 0, 0 will be 7 times and 8th bit will be 8, means 1. So this will be 8 bits. Similarly 7 times 0, 0 will be again. This will be our next 8 bits. And similarly 0, 0 will be again 8 times. And this will be our next 8 bits. in this 8 bit because my byte is of 8 bit and this is my integer so what will I do? I will take the first 8 bits of starting so what value will I get? I will get this one I will ignore this one because it is beyond 8 bits so what did I write here? I wrote 00101100 now if what I have written here is stored in bit here Now whenever I am trying to print it on this line System.out.println Now whenever it will try to print it will change to decimal again It is in binary form now it will change to decimal Now what will be its decimal? 1,2,4,8,16,32 So 32 plus 8 plus 4 32 plus 8 plus 4 So how much is it? 44 So our answer will be 44 Our answer will be 44. 44 will be printed.
I hope you have understood how data truncate works First we took out the binary of 300 and took 8 bits of its starting This is another way to do this If you don't want to take out the binary then you know what you can do You know what will come in B? The number which has come in I which is 300 Mod it with the range of B of B, range of B means range of byte. You know what is the range of byte? From minus 128 to plus 127. And how many characters are these? These are 256 characters, right? Because 2 to the power of 8. Now, the range of bit is 8 bit, so, sorry. Byte's range is 8 bit, so 2 to the power of 8 is a possible combination. So 2 to the power of.
Whatever is coming in your big range, 300. And mod of range of byte. You do this, then also you will get 44, which will be stored in B. Now, what did I call this type of conversion? What do we call explicit conversion? Because it cannot be done by itself. Data will be truncated, so compiler will not do it. For this, you will have to apply casting. How do we cast this? We cast it in byte.
Now, you think, whenever you try to come in small data types from big data types, you will have to use explicit casting. Data truncate will happen. So, think, it will not be the same when you try to bring long in int. Or, you assume, when you try to bring double in int. You will try to bring it in float. So this will happen. And this type of conversion is called narrowing conversion.
We have read two types of conversion. Widening and narrowing. On widening, I forgot to tell you one more thing. Which is our character. Let's say I wrote care C equal to A. I have declared an A, okay? We had read in the last class that internally, this A, how is it stored in memory? Nothing happens, we have a unicode, Who has defined a number of this a, let's say 97, a generally has a number of 97, so when we define a number of 97, then it will go to our memory and store 97 in our container of memory. And we have assigned it as c, we have named it as variable c. Now when we have to retrieve it, the compiler will go back to this container and see what I have to retrieve.
What I mean to say is that internally all the characters are stored as integers only. So we can convert the character into an integer. You know that the character You know that the character that I write here is of 16 bit But our int is of 32 bit That means our integer is big So I can convert the character into int using widening conversion And it will happen automatically, it will happen implicitly So if I write this simple int i equal to c Then we won't have any problem, compiler won't have any problem, it will convert it And when I will try to print it, system.out.println Okay and I print i here, then my value will be 97 because I am printing an integer value and whose integer.
Whatever is the value of this c and what is the value of c? whatever is the value of this unicode of a, then that is 97 so our 97 will be printed I hope you understood narrowing conversion and widening conversion well in this we have to do type casting so we need cast and in widening we don't need cast There is a third type of conversion which comes in narrowing but we read it differently. We call it truncating conversion.
When you take a float value or let's say a double value If you try to change it in integer, then what will happen? To say, our float is also 32 bit and our integer is also 32 bit. Both are of the same range. Okay, double is twice, double 64 bit. But float and int are in the same range, so what will happen? Look, because we have, suppose I have written float f equal to 16.25f.
So I'll have to use casting again. So we'll have to write this format again. I equal to int f. So what did I do? I again typecast this in integer. Now as soon as I do this, You might have understood what will happen. I equal to 16 will be saved in the memory. Because we will ignore its decimal part and only take its integer part.
We call this type of conversion as truncating conversion because we have truncated its decimal part. So, these are all types of conversions. I hope you understood. So, let's see it in code. So here again I have created an empty file by doing demo.java So first of all we will see our implicit conversions Which we know the compiler does by itself So let's see the first conversion, I am writing this in the comments I will do byte to int This should be done automatically So first of all I took a byte variable b equal to 24 And then I took a variable int i So, if I write i equal to b, then will it be done by itself? See, no compiler will come, it means it is done. Now, if I try to print it, i, then what value will come? See, what is our expected value? It should be 24. Okay, so what is 24?
Okay, very good. Now, in the same way, you know, I byte to int, then I can convert short to int as well. We can see that too. But, let's see something interesting. Let's see character to int. You know that this character is of 16 bit and the integer is 32 bit. So, let's say I take a character char c equal to a, which we took as an example. And again I take an int i. We will comment it now. Because I am declaring int i again. So it won't let me declare it twice with the same name. So I have commented the above code. So can I write i equal to c directly?
Now because 24 comes in the byte range, you know what is the byte range? It is from minus 128 to plus 127. So if 24 comes in that range, then my 24 as it is will be printed. So I am expecting 24. Absolutely correct. But what I had said is that if I set 300 here. We had seen the example of 300 because it is out of this range. We take out the binary of 300 and take its first 8 bit. So that 8 bit used to be 44, we had seen that too. So it should be 44 here. See, 44 is here. And you know the second way is to do 300.
So this will also be 44. So you can see it like this. For those who don't know, in java we call it mod operator. Mod operator means you divide 300 by 256 and write the remainder outside. I forgot to tell the operator when I was telling him that I want to mod, so mod means that you divide 300 by 256 and write whatever remainder comes out.
Now you can do the same if you try to convert long into int. So that will also be an explicit conversion. So apart from that, let's see the third type of conversion which is truncating conversion. In this let's say I take a float value. So float f equal to 15.678. Let's say this. And I have to put f here. And before that again I will comment this whole code. And I'll take an int i. So can I directly write i equal to f? Although both are 32 bit but I can't write it.
Do you know why I didn't talk about it? Because these conversions are not possible. Okay, it is not possible in Java. Why it is not possible? Because this Boolean is a special data type. Let's say I wrote Boolean b, let's write bool equal to false. For now, I'll comment it again. I wrote boolean bool equal to false. Now, if I declare an int i, I can put the bool variable in i.
There is no false number. So, I can't even put it without cast. And even if I try to cast, I can't put it. See, there is an error. Cannot cast from boolean to int. It can't cast either. And simply, you can't assign it at all. What's the error? Type mismatch. So if you do it like this, you'll get type mismatch. And if you try to cast, you'll get cast error. Because it won't let you do it.
Now, what if I do this, b is equal to b into 2. I just doubled b and assigned it to b. It should be 100 here. But look, I am getting an error. And what is that error? That is again type mismatch. Cannot convert from int to byte. Now, where did the integer come from? I didn't take int in both these statements.
So let's understand it first and then come back to the code. So now we have to understand Automatic Type Promotion. Now what is Automatic Type Promotion? First of all, let's say I took a byte variable B is equal to 50. Simple. And let's say I took one more byte variable, A is equal to 40. Now suppose, let's take one more. Let's call it A, let's call it B, let's call it C. And let's say I took 100. And I want to do a calculation of all these. This is a widening conversion. So I want to put int I in let's say A into B divided by C. So I want to do this kind of calculation. So what does Java say? Java thinks that here is A, this is a byte, this is a B, this is a byte and C is also a byte. But I know the range of a byte. What is the range of these three? The range of these three is from minus 128 to plus 127.
Java will think that if I keep doing the calculation, then I will not be able to store the byte. So Java does automatic type promotion. Basically, it automatically converts that byte into int when such a calculation is taking place. This means, when this calculation was taking place, this a into b, it converted a and b into byte and into integer at that time. And this calculation took place on integer. So, the answer of a into b, which is 2000, came normally to a. Now it divided 2000 by 100 Now it promoted C to int So that it can do this calculation int divided by int Now 2000 is divided by 100 so the answer is 200 Now 200 is also out of this range But friends, it definitely comes in the integer range. So friends, we will be able to assign this i because i is an integer. Okay? So this is automatic type conversion. And this is just so that you can do this kind of calculation. Because if you write something like a into b divided by c, and a into b only if it goes out of the byte range, then calculation is not possible. So what Java does is automatically promotes some type. So here, in this case, what did it do? In this case, it made byte promote to become an integer.
Now this is confusing because the problem we had in the code was that we wrote byte b equal to 50 and then we wrote b equal to b into 2. And as soon as we wrote this, our compiler error came here in this line. Now why did it come? Again because of the type promotion rule. What did type promotion do? Whenever it saw the calculation b into 2, it thought something is multiplying by bytes. Let me quickly promote this in int. So it promoted this b in int. Now this 50 has become integer 50. Now you did 50 into 2, its value came 100. Now this 100 came, this is integer value. So if I show it with pen also, This B into 2 means 50 into 2.
Remember, we read that this is narrowing conversion. So, for this, what you have to do? Again, you have to do casting. You have to write something B equal to byte B into 2. Something like this. then your problem will be solved. I hope you understood why we need to do casting here. Now, let's see the rules of our type promotions.
They are very simple rules which you can think logically. So the first rule is that any type, be it bite, short, and care. All these will be promoted in int in any calculation. These values are promoted to int. So the first rule is very simple. What does the second rule say? The second rule goes ahead of this. If one operand in any calculation is long, then the entire expression will become long.
You must be able to understand this too. Because long will be the biggest, so everything will become long. The biggest variable, the biggest datatype, everything will be converted into that. Now again, if one operand is, let's say, float. Now because float and int are the same, but float takes decimal point as well. The possibility of float increases to take values. So if one operand is float, entire, sorry, entire expression will be float. will become float. This is your third rule. Now you must be able to understand this obviously. And the fourth rule is also the same. Now it says for double that if one operand is double then what will happen? Now you must have understood. The result is double. Entire expression or result is double. So entire expression is double.
So these are the 4 rules that tell you that the biggest value will be changed in your code. Now to prove this, we have a code. So let's see that first. First of all, let's solve this problem. We said we can solve it by type casting. So if I write byte here, So, see, this problem should be solved.
And I have printed this expression one by one F into B, I divide by C and D into S. And I have printed the whole result. So let's try to figure out what will be the result. So first of all, what I said was F is float and B is byte So when you multiply float with byte Then your resulting expression should be float Because float is big So this whole expression will come in float variable Same I divide by C I is your integer and C is your character So when you divide I by C Then your whole expression should come in integer format So your character will become integer Same, when you multiply D with S, what is D? Your double. See, you can write it like this. Even if you want, without adding 0, you will.
When you add these three expressions together So first of all, what did you do? F into B Float was added by I by C From integer So when you add float Float will be added from integer So what will be your product? Sorry, what will be your summation? It will be your float Right? Because integer will convert you to float And then when you add this whole float from double So what will be your float plus double? your final answer will be double. And double will be your result.
