1.5 Data types

There are 8 primitive data types. All other variable types are objects that are created in classes.

These primitive data types are always lower case. All object types are uppercase.
So int x; is a primitive data integer. Integer y is an Integer object.

Data Type Name Size Data Type Range Suffix character
int
4 byte integer type -2,147,483,648 to 2,147,483,647 --default--
no i or I
short 2 bytes integer type Do not use -32,768 to 32,767 no s or S
long 8 bytes integer type -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
L
byte 1 byte integer type -127 to +128 no b or B
char 2 bytes
(because of Unicode)
one character: 'a' or '\n'.
Not "a" 'ab' or "ab"
float 4 bytes floating point / decimals
Not used very often.
3 .4e-45 to 3.4e+38
(8 decimal places)
f or F
double 8 bytes floating point / real number 1 .7e-324 to 1.7e+308
(16 decimal places)
-- default --
d or D
boolean 1 byte? True/False (not equal to 1 or 0)

You must add the correct letter at the end in order to specify something other than the default.
This means, whenever you use a FLOAT or a LONG, you have to write F or L.
For example, float depth = 1.235F; long startTime = 12345678L;
The other data types (byte, char, boolean) don't need suffix characters.

Strings are objects, but they act like primitive data types much of the time

Integer vs Floating Point Math

IntegerFloating Point
divide by zerocrashes programresults in "infinity"
overflow
(number too big)
wraps around to the biggest negative numberresults in "infinity"
accuracyexactrounding errors
illegal mathonly with doublesMath.sqrt(-1) → NaN (not a number)
divide 0 by 0 → NaN

NOTE: if x is NotaNumber and y = x; y == x is still false. One NaN is never equal to another NaN!

Type in and run this program: MathExamples.java

Entering other base numbers:

Continue on to read about Casting