Showing posts with label Casting. Show all posts
Showing posts with label Casting. Show all posts

Friday, October 20, 2017

Division Operator In Java

Hi All,

Today I'm going to teach you guys how to use division operator in Java. As we know division operator use to divide basically two number and get the answer. Symbol of division operator is / in java and most of other programming languages. In natural languages we use same symbol but there are some different ways people use to write it with numbers to symbolize division operation.

ex
½  1/2

Following is some mathematical examples for how to use division operator

ex

1/2 = 0.5
3/4 = 0.75
2/4 = 0.5

Let's see how to do this 



x = 1 and y = 2 so x/y must be 0.5 but we got 0 as the answer why is this happening ?

by default compiler take answers of integer variables' operation as int so int data type can't keep floating point so it will return the only integer value it has as 0

we can fix this by casting x and y to double.


If we use double variables we can also fix that issue.


See you guys in my next post bye ....

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.