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

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.

主站蜘蛛池模板: 清远市| 宁都县| 寻乌县| 榕江县| 宁乡县| 永城市| 长泰县| 宜兰县| 东城区| 景宁| 屯门区| 米林县| 古蔺县| 白银市| 会昌县| 冀州市| 桐庐县| 太原市| 顺昌县| 汤原县| 永新县| 盐山县| 迁西县| 八宿县| 定远县| 井陉县| 萨迦县| 桂东县| 长岛县| 尉犁县| 涞源县| 正蓝旗| 武强县| 陆丰市| 霍州市| 巫溪县| 阳西县| 上犹县| 武邑县| 凌源市| 交口县|