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.



1 comment:

  1. Very good information. we need learn from real time examples and for this we choose good training institute, I'm interested to know about java programming which is quite interesting. i need a good training institute for my learning .. so am attending the free demo class which is provided by Apponix Technologies.
    https://www.apponix.com/Java-Institute/Java-Training-Institute-in-Bangalore.html

    ReplyDelete