Real Numbers

     

Real Numbers

The Java floating point types are float and double . Floating point types are numbers with a decimal place.

float

Occupies 32 bits or 4 bytes, with 6 or 7 significant digits

Default value of 0.0

Range : 6-7 significant decimal digits

Example : 3.1459F

You specify the float by placing an F literal after the float value. If you don't, the compiler will treat your number as a double .

Java floating point numbers follow the IEE754 specification, which includes positive and negative signed numbers, positive and negative zero, and a special value referred to as NaN (Not a Number).

NaN is used to represent the result of an illegal or invalid operation, such as an attempt to divide by zero. The Float and Double wrapper classes both have a method called isNan() , which returns a true or false depending on whether the specified number is Not a Number.

double

Occupies 64 bits or 8 bytes, with 14 or 15 significant digits

Default value of 0.0

Range : 15 significant decimal digits

Example : 67, 2.0E10, 111,222,333,444.567, 67.99D

The double value in the preceding example includes an E. This is engineering notation for an irrational number representing the base of the natural system of logarithms. It has an approximate numerical value of 2.7182818284. This notation was first used in the mid-seventeeth century and has been calculated to 869,894,101 decimal places. You can use E in your double values like this: the number 8.9E5 is another way of writing 8.9 * 10 5 .

You can place a literal D after the double value in order to explicitly indicate in your code that you mean for the value to be a double , as distinct from a float. This isn't necessary, however, as double is the compiler's default.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net