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

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.

主站蜘蛛池模板: 晋江市| 大兴区| 蒲江县| 密云县| 甘肃省| 鹤山市| 宁化县| 怀柔区| 陆川县| 宁远县| 峡江县| 双江| 浠水县| 左云县| 乐业县| 松阳县| 麻阳| 临海市| 中超| 兴业县| 霸州市| 滨州市| 蓬溪县| 盐山县| 龙井市| 高陵县| 台东市| 老河口市| 香港| 涟水县| 青海省| 襄城县| 大关县| 哈密市| 顺平县| 阿拉善左旗| 安远县| 贵定县| 东乡| 旬邑县| 左云县|