Java Data Types
Every variable in Java has a type. The type decides which values the variable can hold, how much memory it uses, and which operations make sense. Java has two groups of types: eight primitive types for simple values, and reference types for everything else, such as String, arrays and the classes you will write yourself.
The eight primitive types
| Type | Size | Holds | Example |
|---|---|---|---|
byte | 1 byte | whole numbers from -128 to 127 | byte b = 100; |
short | 2 bytes | whole numbers from -32,768 to 32,767 | short s = 3000; |
int | 4 bytes | whole numbers up to about 2.1 billion | int n = 250000; |
long | 8 bytes | very large whole numbers | long l = 9000000000L; |
float | 4 bytes | decimal numbers, about 7 digits | float f = 3.5f; |
double | 8 bytes | decimal numbers, about 15 digits | double d = 3.14159; |
boolean | 1 bit of information | true or false | boolean ok = true; |
char | 2 bytes | one character | char c = 'A'; |
In everyday programs you mostly use int for whole numbers, double for decimals, boolean for yes-or-no answers and char for single characters.
public class Main {
public static void main(String[] args) {
int students = 32;
long worldPopulation = 8_100_000_000L;
double price = 49.99;
boolean open = true;
char grade = 'B';
System.out.println(students);
System.out.println(worldPopulation);
System.out.println(price);
System.out.println(open);
System.out.println(grade);
}
}32 8100000000 49.99 true B
Two details: a long value that does not fit in an int needs an L at the end, and a float value needs an f. A char is written in single quotes, while text (a String) uses double quotes.
Limits of the number types
Each whole-number type has a smallest and a largest value. Java keeps them in constants such as Integer.MAX_VALUE. If a calculation goes past the largest value, it wraps around to the negative side without any error, which is a classic bug:
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Long.MAX_VALUE);
int big = Integer.MAX_VALUE;
big = big + 1;
System.out.println(big);
}
}2147483647 9223372036854775807 -2147483648
When numbers can become very large, use long. Decimal numbers have a different limit: they are stored in binary, so some values like 0.1 cannot be stored exactly. You will see this in the Operators topic.
Reference types
String is not a primitive type. It is a class, and a String variable holds a reference to a text object. Reference types have methods you can call with a dot:
public class Main {
public static void main(String[] args) {
String word = "Java";
System.out.println(word.length());
System.out.println(word.toUpperCase());
}
}4 JAVA
Primitive values have no methods. A reference variable can also hold the special value null, which means "no object"; a primitive variable never can. The Strings topic covers text in detail, and the Wrapper Classes topic shows how each primitive type also has a class version.
Practice exercises
- Make one variable of each primitive type and print them all.
- Print
Short.MAX_VALUEandByte.MIN_VALUE. - Store the number of seconds in 100 years in a
longand print it. (Use100L * 365 * 24 * 60 * 60.)