- Java Programming for Beginners
- Mark Lassoff
- 463字
- 2021-07-02 15:22:45
Floating point variables
When we're simply counting and manipulating whole objects, integers are fantastic. However, sometimes we need to deal with numbers in a more mathematical sense, and we need a data type that will allow us to express ideas that are not entirely whole numbers. Floating-point numbers, or floats, are a Java primitive type that allow us to express numbers that have decimal points and fractions. In this section, we'll modify some float and integer variables side by side to see how they are similar and different.
Let's create a new Java project (you know the drill by now) and call it FloatingPointNumbers. Let's start by declaring two variables: one integer (iNumber) and one float (fNumber). As we know, once we've declared these variables, we're free to modify and assign values to them in our Java program later. This time, let me show you that we can also modify and assign to these variables in the same line that they're declared. So where I have declared my iNumber integer variable, I'm free to immediately give it the value of 5:
package floatingpointnumbers; public class FloatingPointNumbers { public static void main(String[] args) { int iNumber = 5; float fNumber; } }
Notice that if we try and do something very similar with our float variable, NetBeans will yell at us, by displaying a light bulb and red dot on the left-hand side:

In fact, if we attempt to compile our program, we'll get a legitimate compiler error message:

Let's analyze why this happens. When we use an explicit number in Java, that is, typing out the digits rather than working with a variable, that explicit number is still given a type by Java. So when we type out a number without any decimal places, the type that the number is assumed to be is an integer. So our assignment works just great. However, a number with decimal places is assumed to be of this type and is called double; it's a sister type of the float data type, but it's not quite the same. We'll talk about double a little later. Right now, what we need to do is tell Java to treat 5.5 as a float type number instead of double. To do this, all we need to do is put f after the digits, as follows:
float fNumber = 5.5f;
You'll see that the bulb and red dot have disappeared. To make sure we get the syntax right, let's give our program some super basic functionality. Let's use System.out.println() to print our integer number and then our floating-point number variable in sequence:
System.out.println(iNumber); System.out.println(fNumber);
When we build this program, our compiler error goes away, and when we run it, we see the two assigned values as expected. Nothing too exciting there:

- Expert Android Programming
- MySQL數據庫基礎實例教程(微課版)
- 從0到1:Python數據分析
- bbPress Complete
- Android Wear Projects
- GameMaker Essentials
- 軟件體系結構
- 深度探索Go語言:對象模型與runtime的原理特性及應用
- Troubleshooting Citrix XenApp?
- 監控的藝術:云原生時代的監控框架
- Visual C++開發寶典
- ArcPy and ArcGIS(Second Edition)
- Design Patterns and Best Practices in Java
- Apache Solr for Indexing Data
- Instant Pygame for Python Game Development How-to