Variables and numeric data types

 

Since there’s little we can do with numeric values alone, Java provides us with the ability to define and work with variables.  As variables are representations of values, we expect to be able to write more complex programs using variables, and indeed this is true.  In Java, variables could refer to numeric or non-numeric values.  Our attention will first be directed towards variables that hold numeric values.  These can naturally be thought of as falling in one of two groups: the integers (whole numbers) or real numbers (those with decimals).  We’ll ignore complex numbers.    Java defines data types to make the distinction between whole numbers and decimals.  In fact, each variable must have a type in Java.  There are several data types in Java to represent numeric values (many more than we’d expect), but for now we’re going to consider only the four most common ones:

 

               Type                     What it represents

int                       A whole number, positive or negative approximately in the range -2.1E9 and +2.1E9 (if scientific notation escapes you, 2.1E9 is 2.1B)

long                     A whole number, positive or negative approximately in the range -9.2E18 and +9.2E18.

 

float                   A decimal number approximately in the range -3.4E38 to +3.4E38 with 6 or 7 digits of precision

double                 A decimal number approximately in the range -1E308 to +1E308 with 14 or 15 digits of precision