Loading lessons...
Java Numbers
Java Numbers
Java provides numeric types for whole numbers and decimal numbers.
Integer types
Whole numbers can be stored in byte, short, int, or long:
byte myByte = 100;
short myShort = 5000;
int myInt = 100000;
long myLong = 15000000000L;
Note the L at the end of the long value. Java needs it to know the number is a long.
Decimal types
float and double store numbers with decimals:
float myFloat = 5.75f;
double myDouble = 19.99d;
The f suffix marks a float, and the d suffix marks a double. The d is optional because double is the default.
Choosing a type
- Use
intfor everyday whole numbers. - Use
longfor numbers larger than about 2 billion. - Use
doublewhen you need decimals. - Use
floatto save memory when decimals with lower precision are fine.
Scientific numbers
double also supports scientific notation with e:
double x = 1.5e3; // 1.5 * 1000 = 1500.0
TL;DR
- Whole numbers:
byte,short,int,long. - Decimals:
float,double. longvalues end withL;floatvalues end withf.