<< Chapter < Page Chapter >> Page >

Floating-point types

Floating-point types are a little more complicated than whole-number types. I found the following definition of floating-point in the Free On-Line Dictionary of Computing :

A number representation consisting of a mantissa, M, an exponent, E, and a radix (or "base"). The number represented is M*R^E where R is the radix.

What does this really mean?

I'm not going to get into a long discussion about floating-point values at this point. If you are really interested, you will find a detailed discussion inlesson # 905 at (External Link) .

Advantages of floating-point types

One advantage of floating-point types is that they can be used to maintain fractional parts in data values.

Another advantage of floating-point types is that a very large range of values can be represented using a reasonably small amount of computer memory for storage of the values.

For example (assuming that I counted the number of digits correctly) Figure 3 shows how to represent a very large value and a very small value as a floating-point type.

Figure 3 . Representing a large range of values.

The very large value: 62357185000000000000000000000000000000.0

can be represented as

6.2357185E+37

The very small value:

0.0000000000000000000000000000062357185

can be represented as 6.2357185E-30

When would you use floating-point?

If you happen to be working in an area where you need to keep track of fractional parts (such as the amount of hamburger in a package) , have to work with extremely large numbers (distances between galaxies) , or have to work with extremely small values (the size of atomic particles) , then you will need to use the floating-point types.

Also, if you will be doing computations involving division, floating-point is often easier to work with because division with integer typescan be problematic. (The remainder is discarded in integer division, which can cause arithmetic accuracy problems.)

Two floating-point types

Java supports two different floating-point types:

  • float
  • double

These two types differ primarily in terms of the range of values that they can support and the number of significant digits used in the representation of those values.

Figure 4 shows the smallest and largest values that can be accommodated by each of the floating-point types. Values of either type can be either positive or negative.

Figure 4 . Value range for floating-point types.
  • float: 1.4E-45 to 3.4028235E38
  • double: 4.9E-324 to 1.7976931348623157E308

Exclude type float

As you saw earlier , the authors of the exam have excluded type float including only type double (of the two floating-point types) in the exam. If one of the floating-point types is to be excluded, type double is the logical choice to keep. In many ways, it is the default floating-point type inJava. For example, a literal value, such as 3.14159, is considered to be type double unless you write code to force it to be treated as type float .

The boolean type

The boolean type is the simplest type supported by Java. It can have only two values:

  • true
  • false

Generally speaking, about the only operations that can be applied to an instance of the boolean type are to change it from true to false, and vice versa. The boolean type is commonly used in some kind of a test to determine what to do next.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ap computer science a, clarification of the java subset. OpenStax CNX. Oct 03, 2015 Download for free at https://legacy.cnx.org/content/col11279/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ap computer science a, clarification of the java subset' conversation and receive update notifications?

Ask