How to use primitive data types
Store whole numbers
int is the usual whole-number type. Use an L suffix for a long literal beyond the int range.
int students = 30;
long worldPopulation = 8_000_000_000L;
Store decimal numbers
double is the normal floating-point type. A float literal needs an F suffix.
double price = 19.95;
float ratio = 0.5F;
Store characters and booleans
A char holds one UTF-16 code unit in single quotes. A boolean is either true or false.
char grade = 'A';
boolean enrolled = true;
Know the primitive families
- Integers:
byte,short,int, andlong. - Floating point:
floatanddouble. - Other primitives:
charandboolean. Stringis a class, not a primitive type.