Can we cast an int value into byte type variable? what if the value of int is larger than byte?

By | August 22, 2019

In this post we will discuss about one of the basic  java interview question and question is that Can we cast an int value into byte type variable? what if the value of int is larger than byte?

Yes, we can cast but int is 32 bit long while byte is 8 bit long in java so when you cast an int to byte higher 24 bits are lost and a byte can only hold a value between -128 to 127

Let’s understand above concept using an example

Sample output

125
-30

You may also like:

History of Java
What are JVM, JRE and JDK in Java?
Differences between Java EE and Java SE

That’s all about Can we cast an int value into byte type variable? what if the value of int is larger than byte?
If you have any feedback or suggestion please feel free to drop in blow comment box. 

Leave a Reply

Your email address will not be published. Required fields are marked *