float

The type float refers to floating-point numbers stored in 32 bits, as described in the IEEE standard reference 754.

The justification for using single-precision variables used to be that arithmetic operations were twice as fast as on double precision variables. With modern extensively pipelined processors and wide data buses between the cache and CPUs, the speed differences are inconsequential. The reasons for using floats are to minimize storage requirements when you have a very large quantity of them or to retain compatibility with external data files.

Floats are best avoided if possible, because they have such limited accuracy. They are in Java because they are supported in hardware so it costs little, and provides compatibility with existing code and expectations.

range of values: The type float provides numbers that can range between about –3.4E38 to 3.4E38 (i.e., 340,000,000,000,000,000,000,000,000,000,000,000,000) with about six to seven significant figures of accuracy. The exact accuracy depends on the number being represented.

literals: The simplest way to understand what is allowed is to look at examples of valid float literals, as follows:

1e1f  2.f  .3f  3.14f  6.02e+23f

A suffix of “F” or “f” is always required on a float literal. A common mistake is to leave the suffix off the float literal, as follows:

float cabbage = 6.5;
Error: explicit cast needed to convert double to float.

The code must be changed to the following:

float cabbage = 6.5f;

Also, a double literal cannot be assigned to a float variable without a cast, even if it is within the range of the float type. This is because some precision in the decimal places may potentially be lost.

That concludes the description of the eight primitive types, and we now continue with an explanation of how each of these eight primitives also has a class type to represent it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.175