0% found this document useful (0 votes)
20 views2 pages

02 Floating

Uploaded by

New Bie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

02 Floating

Uploaded by

New Bie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

FLOATING-POINT DATA

A floating-point value is a number that can have a fractional part (e.g. 3.14). A floating-point
variable must be declared as either float or double. Seasoned Java programmers use
double unless memory conservation is an issue. The sizes of these data types are shown below.
Java stores floating-point data in binary according to the IEEE 754 specification.

Java Floating-Point Data


Data Type Size Can Hold a Value up to
float 32 bits approximately 7 significant digits
double 64 approximately 15 significant digits

Floating-point literals are typed as a sequence of digits, preceded by an optional + or – sign, with
the fractional part identified by a decimal point. The underscore (_) can be used as a group
separator.

Example
double a = 150.;
double b = +150.;
double c = -.015;
double d = 1_000;

Although it is not required by Java’s grammar, seasoned programmers emphasize the decimal
point by typing a digit on both of its sides.

Example
double a = 150.0;
double b = +150.0;
double c = -0.015;
double d = 1_000.0;

Floating-Point Data Page 1


You can use a suffix on the floating-point literal to indicate its data type – D for a double or F
for float. Lacking either, the literal is taken to be a double value.

Example
double a = 1_024.0D;
float b = 1_024.0F;

Example

Wrong!
float x = 1_024.0; The 64-bit literal 1_024.0 may not be stored in the
32-bit variable x.

A floating-point literal may use scientific notation.

Examples

double a = -1.5E2; a -150.0


double b = -1.5E-2; b -0.015
double c = +1E+6; c 1000000.0
float d = +1.5E2F; d 150.0

Exercises

Write the Java statement for each.

1. Declare d1 a double initialized to negative one million using scientific notation.

2. Declare d2 a double initialized to negative one million without using scientific notation.

3. Declare d3 a double initialized to one millionth using scientific notation.

4. Declare d4 a double initialized to one millionth without using scientific notation.

5. Declare f1 a float initialized to one million using scientific notation.

6. Declare f2 a float initialized to one million without using scientific notation.

Floating-Point Data Page 2

You might also like