官术网_书友最值得收藏!

Behavior difference between integer and float data types

Now, rather than assigning explicit values to our variables, let's do some basic arithmetic so we can see how integer and floats, when modified in Java, behave differently. In Java, both float and int are primitive types, the logical building blocks of the programming language. This means we can compare and modify them using mathematical operators, such as division.

We know that if we attempt to divide one integer by another, we'll always get a whole number as a result, even if the rules of standard mathematics don't make that the expected result. If we divide a floating-point number by another floating-point number, however, we'll get a more mathematically accurate result:

package floatingpointnumbers; 
 
public class FloatingPointNumbers { 
 
    public static void main(String[] args) { 
        int iNumber = 5/4; 
        float fNumber = 5.0f/4.0f; 
        System.out.println(iNumber); 
        System.out.println(fNumber); 
    } 
} 

The following is the output of the preceding code:

Sometimes, Java will let us do things that might not be such a good idea. For example, Java lets us set the value of our floating-point variable fNumber to one integer divided by another instead of one floating-point number divided by another:

int iNumber = 5/4; 
float fNumber = 5/4; 

Because the computation on the right-hand side of our equals sign occurs before the value of our floating-point variable fNumber changes, we're going to see the same output in both the calculations of 5/4. This is because both the 5's and 4's are integer variables. So when we run our program, even though fNumber remains a floating-point number (as we can tell because it prints out with the decimal place), its value is still set to the rounded down whole number of 5/4:

Solving this problem is pretty straightforward; we simply need to change one of our integer values to be a floating-point number by appending f to it:

int iNumber = 5/4; 
float fNumber = 5/4.0f; 

Now the computation will know how to proceed with a decimal place division:

This becomes a little trickier and more important to navigate properly when we stop working with explicitly declared numbers and begin working with variables.

Let's declare two integer variables now. I'll just call them iNumber1 and iNumber2. Now, rather than attempting to set the value of fNumber to one explicitly declared number divided by another, we'll set its value to iNumber1/iNumber2, and we'll just print out the results stored in fNumber:

package floatingpointnumbers; 
 
public class FloatingPointNumbers { 
     
    public static void main(String[] args) { 
        int iNumber1 = 5; 
        int iNumber2 = 6; 
        float fNumber = iNumber1/iNumber2; 
 
        System.out.println(fNumber); 
    } 
} 

When we run this program, because once again we're dividing one integer by another, we're going to see the rounding down phenomenon. The value being stored in our floating-point variable is 0.0, the rounded down result of 5/6:

If we were working with explicitly declared numbers, we would solve this problem by changing one of the two integer numbers to be treated as a floating-point number by simply putting a decimal place and f after it. In this context, using iNumber2f is not an option because rather than thinking that we're asking it to treat iNumber2 as a floating-point number, Java now believes it's looking for a variable called iNumber2f, which certainly doesn't exist within this context.

主站蜘蛛池模板: 星子县| 城口县| 双城市| 凤庆县| 五河县| 钦州市| 沙河市| 东山县| 富民县| 麻江县| 瓦房店市| 蒙城县| 泰顺县| 渭源县| 阳山县| 六盘水市| 定结县| 四会市| 岐山县| 永兴县| 铜梁县| 铅山县| 本溪| 县级市| 和顺县| 海安县| 务川| 钟山县| 林周县| 龙海市| 南丹县| 曲松县| 大连市| 金乡县| 西宁市| 平湖市| 金寨县| 太湖县| 上林县| 曲麻莱县| 大同市|