Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Java has exactly eight primitive types. Instead of memorising a list, let's sort them into families. Which ones hold whole numbers?
byte, short, int and long. Do they only differ in size?
Yes, and size decides range. A byte has 8 bits, so it can hold only 256 different values, from -128 to 127. An int has 32 bits and reaches about 2.1 billion.
Why does a byte go down to -128 but only up to 127?
Zero uses one of the non-negative slots, so the positive side ends one short. Every whole-number type follows the same pattern.
So the other families are float and double for decimals, char for a single character and boolean for true or false?
Exactly: four families, eight types. Each is a lowercase keyword and each holds one plain value.
Type float price = 5.5; and compile it. What happens?
Error: possible lossy conversion from double to float. But 5.5 is such a small number!
The size of the number is not the problem. Every decimal literal is a double by default, so you add an f and write 5.5f. Long works the same way: a literal past 2.1 billion needs an L.
And if a calculation goes past the limit, like Integer.MAX_VALUE + 1?
It wraps around to -2147483648 without any error. That is called overflow, and Java will not warn you about it.
So when a total might get really big, I should use long from the start.
Yes. And remember that decimals are approximations: in Java, 0.1 + 0.2 prints 0.30000000000000004.
What do you think System.out.println('A' + 1); prints?
The letter B?
It prints 66. A char is stored as its Unicode code, and 'A' is 65. Adding the int 1 gives an int.
So to get B we have to turn 66 back into a char, with (char) ('A' + 1)?
Perfect. One last question: if we write int b = a; and then change b, does a change too?
No. b got a copy of the value, so the two variables are separate.
That copying behaviour is true of every primitive type. Hold on to it, because non-primitive types work differently.
whole numbers (byte, short, int, long), decimals (float, double), a character (char) and logic (boolean).
each type has the same size on every platform, and its size fixes its range.
whole-number literals are int and decimal literals are double, so long needs L and float needs f.
a whole-number result that passes its limit wraps around silently, with no error.
float and double hold approximations, and double is about twice as precise.
assigning one primitive variable to another copies the value, so the two stay independent.
All eight primitive types declared with suitable values and printed:
public class PrimitiveTour {
public static void main(String[] args) {
byte floorNumber = 7;
short year = 2024;
int citizens = 1_400_000;
long distanceToSunKm = 149_600_000L;
float price = 99.5f;
double pi = 3.141592653589793;
char grade = 'A';
boolean isJavaFun = true;
System.out.println("byte floorNumber = " + floorNumber);
System.out.println("short year = " + year);
System.out.println("int citizens = " + citizens);
System.out.println("long distanceToSunKm = " + distanceToSunKm);
System.out.println("float price = " + price);
System.out.println("double pi = " + pi);
System.out.println("char grade = " + grade);
System.out.println("boolean isJavaFun = " + isJavaFun);
}
}The exact range of every type, read from the constants of its wrapper class instead of typed by hand:
public class PrimitiveRanges {
public static void main(String[] args) {
System.out.println("byte : " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE + " (" + Byte.SIZE + " bits)");
System.out.println("short : " + Short.MIN_VALUE + " to " + Short.MAX_VALUE + " (" + Short.SIZE + " bits)");
System.out.println("int : " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE + " (" + Integer.SIZE + " bits)");
System.out.println("long : " + Long.MIN_VALUE + " to " + Long.MAX_VALUE + " (" + Long.SIZE + " bits)");
System.out.println("float : largest is " + Float.MAX_VALUE + " (" + Float.SIZE + " bits)");
System.out.println("double: largest is " + Double.MAX_VALUE + " (" + Double.SIZE + " bits)");
System.out.println("char : codes " + (int) Character.MIN_VALUE + " to " + (int) Character.MAX_VALUE + " (" + Character.SIZE + " bits)");
}
}Life at the limits: int overflow, and how float and double round the same division:
public class LifeAtTheLimits {
public static void main(String[] args) {
int max = Integer.MAX_VALUE;
System.out.println("int max : " + max); // 2147483647
System.out.println("int max + 1 : " + (max + 1)); // -2147483648 (overflow)
System.out.println("as long, max + 1: " + (max + 1L)); // 2147483648
float third = 1.0f / 3;
double betterThird = 1.0 / 3;
System.out.println("float 1/3 : " + third); // 0.33333334
System.out.println("double 1/3 : " + betterThird); // 0.3333333333333333
System.out.println("0.1 + 0.2 : " + (0.1 + 0.2)); // 0.30000000000000004
}
}A char is stored as a number, and copying a primitive copies only its value:
public class CharAndCopies {
public static void main(String[] args) {
char letter = 'A';
int code = letter; // a char fits into an int: 65
char next = (char) (letter + 1); // 66 turned back into a char
char fromCode = 97;
System.out.println(letter + " is stored as the number " + code);
System.out.println("letter + 1 as a number: " + (letter + 1));
System.out.println("letter + 1 as a char : " + next);
System.out.println("char 97 is : " + fromCode);
int a = 5;
int b = a; // b gets its own copy of 5
b = 10; // changing b leaves a alone
System.out.println("a = " + a + ", b = " + b);
}
}One, two, four and eight, the whole numbers grow straight: byte, short, int and long, each one twice as strong.
Eight siblings live in the House of Java. The four Counters, Byte, Short, Int and Long, each sleep in a room twice the size of the last. The twins Float and Double measure everything but never quite exactly, and Double is twice as careful. Char wears a letter on his shirt but keeps a number in his pocket, and Boolean only ever answers yes or no.
Big Sharks In Lakes Find Delicious Crunchy Biscuits: byte, short, int, long, float, double, char, boolean, in family and size order.
L.F.Q. - L for long literals, F for float literals, Quotes (single) for char literals.
Term
How many primitive data types does Java have?
Click to flip
Term
What is the range of a byte?
Click to flip
Term
Why does `float f = 5.5;` fail to compile?
Click to flip
Term
What does `Integer.MAX_VALUE + 1` evaluate to?
Click to flip
Term
What number does Java store for the char 'A'?
Click to flip
Term
Which type should you use for whole numbers that can pass 2.1 billion?
Click to flip
One of the eight built-in types (byte, short, int, long, float, double, char and boolean) that hold a single simple value directly.
The smallest unit of computer memory, holding a 0 or a 1; eight bits make one byte.
The smallest and largest values a type can store, fixed by the number of bits it uses.
A value written directly in code, such as 42, 5.5f, 'A' or true.
What happens when a whole-number result goes past its type's range and silently wraps around to the other end.
How many significant digits a decimal type keeps accurately, about 7 for float and 15 to 16 for double.
The worldwide numbering of characters that a char stores; 'A' is 65 and 'a' is 97.
A class such as Integer or Double that matches a primitive type and provides constants like Integer.MAX_VALUE.
Java has exactly eight primitive data types (byte, short, int, long, float, double, char and boolean). Each is a built-in keyword with a fixed size, and that size decides which values it can hold.
Primitive types are the simplest data types in Java. They are built into the language, written as lowercase keywords, and each variable of a primitive type holds one plain value. The eight types form four families: whole numbers (byte, short, int, long), decimal numbers (float, double), single characters (char) and true/false values (boolean). Because every type has a fixed size, every type also has a fixed range. Choosing a type well means knowing those ranges, the literal rules (L for long, f for float, single quotes for char) and what happens when a value goes past its limit.
A primitive type is built into the Java language itself. Its name is a lowercase keyword (int, not Integer), a variable of that type holds exactly one simple value, and the value is stored directly in the variable. Java has exactly eight primitive types and you cannot create new ones. Everything else, such as String, arrays and your own classes, is a non-primitive type, covered in the next lesson.
Every extra bit doubles the number of values a type can hold. One bit stores the sign, and zero sits on the non-negative side, so every whole-number range has one more negative value than positive: a byte runs from -128 to 127. A boolean carries one bit of information, but how much memory it really uses is left to the JVM. The sizes are the same on every computer (an int is 32 bits on Windows, Linux and macOS alike), which is part of what makes Java portable.
250, is an int by default. A value too big for an int needs an L at the end: long big = 9876543210L;. Without the L, the compiler reports "integer number too large".5.5, is a double by default. To store it in a float, add f: float f = 5.5f;. Writing float f = 5.5; is a compile error, because a double cannot be squeezed into a float without risking precision.char literal uses single quotes and holds exactly one character: 'A'. Double quotes make a String, so "A" is text, not a char.1_000_000 is the same number as 1000000.L, because a small l looks almost exactly like the digit 1.Integer.MAX_VALUE + 1 gives -2147483648, the smallest int, with no error and no warning. If a value can grow past about 2.1 billion, use long.float and double store binary approximations of decimals. 1.0f / 3 prints 0.33333334, 1.0 / 3 prints 0.3333333333333333, and even 0.1 + 0.2 prints 0.30000000000000004. Use double for everyday decimals and never assume a decimal result is exact. Programs that handle money usually count whole paise or cents in a long, or use Java's BigDecimal class, which stores decimal values exactly.A char stores a Unicode code, a whole number from 0 to 65,535. 'A' is stored as 65 and 'a' as 97, so every lowercase letter is exactly 32 above its capital. That is why 'A' + 1 evaluates to the int 66, while (char) ('A' + 1) turns the result back into the letter B.
Copying a primitive copies the value itself. After int a = 5; int b = a; b = 10;, the variable a is still 5, because b received its own copy. Non-primitive types behave differently, as later lessons show.
int for everyday whole numbers, and long once values can pass 2.1 billion (populations, time in milliseconds).double for everyday decimals; float only when memory matters, such as huge arrays of measurements.byte and short for huge arrays of small values and for raw file or network data.char for one letter, digit or symbol, and boolean for yes/no flags such as isLoggedIn.