Sunday, December 20, 2015

Casting part 3

Hi All...!

Today I'm going to show you how to cast int data type's variables to long data type. If someone don't have any idea about Java data type please read the post Data Types In Java. Again I like to recall your memory "Casting" means converting large ranged data type's variables to small  ranged data type if it is comes under the range of the small data type.

Let's see the code,


Same thing happens if the value of the long variable exceed the limit of the int data type please read the post about casting Casting part 1 and Casting part 2

Sunday, December 13, 2015

Casting part 2

Hi All...!

Today I'm going to teach you guys how to cast "int" data type variables in to "short". Keep it in mind we do casting to convert large capacity data type's variables to smaller capacity data type's variables.

Let's cast "int" to "short"



Let's see what will happen when int value exceed range of short value (short value range is -32768 to 32767 short gets this range because short is 16 bit data type and 2^16  )


32767 is inside the short range, Let's do the same coding with the 32768 and find out what will happen.


Answer is -32768 we got this value because 2^16 is 65536 and 32768 - 65536  = -32768.

Like this if we cross the short range from minus side we will get,



-32769 + 65536 = 32767.

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.