Wednesday, July 24, 2013

Declaring Floating-Point Variables

You declare floating-point variables in a similar way to what you’ve already used for integers. You can declare and initialize a variable of type double with the following statement:

double sunDistance = 1.496E8;

This declares the variable with the name sunDistance and initializes it with the appropriate value. Declaring a variable of type float is much the same. For example:

float electronMass = 9E-28F;

This defines and initializes the variable electronMass. You can, of course, declare more than one variable in java program of a given type in a single statement:

float hisWeight = 185.2F, herWeight = 108.5F;

Remember that you must put the F or f at the end of literals of type float. If you leave it out, the literal will be of type double, and the compiler won’t convert it automatically to type float.

No comments:

Post a Comment

Thanks for Commenting......