Showing posts with label covert one variable belong to specific data type to another data type. Show all posts
Showing posts with label covert one variable belong to specific data type to another data type. Show all posts

Wednesday, December 9, 2015

Casting part 1

Hi All...!

Today I'm going to teach you how to do casting in Java. Casting is a method we can use to covert one variable belong to specific data type to another data type. To do this that variable must be fit in to the range of the data type we are going to convert.


Let's see an example,


As we see in above code we can assign "int" value to the "byte" if that value is fit to "byte" data type range.

Range of  the "byte" data type is -128 to +127 if go out of the range you will get output -256 or +256 (2^8 byte is 8 bit data type ).

As a example if we use int value as +128 and cast it to byte you will get the out put value as -128.


Let's assign -129 to the int value after you do the casting you will get the out put value as +127.