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 data types. Can you name them all?
byte, short, int and long for whole numbers, float and double for decimals, char for a character and boolean for true or false.
Perfect. Why do you think Java needs four different integer types?
Because they have different sizes, 8, 16, 32 and 64 bits, so they hold different ranges.
Right. A byte can hold only -128 to 127. And these sizes are the same on every computer. Why does that matter?
Then a Java program gives the same results wherever it runs.
Exactly. That is part of what makes Java platform independent.
If I write float price = 49.99; the compiler refuses. Why?
Because 49.99 is a double literal by default, and a double cannot go into a float without losing precision.
So how do we fix it?
Add an f at the end, so it becomes 49.99f.
Good. And what about long people = 8000000000; without any suffix?
That number is bigger than the int range, so it has to be written with an L, like 8000000000L.
Exactly. Whole-number literals are int by default, and decimal literals are double by default.
Suppose char ch = 'A'. What does System.out.println(ch + 1) print?
The letter B?
It prints 66. In arithmetic a char is used as its Unicode number, and 'A' is 65. To get B, you cast the result back: (char) (ch + 1).
What happens if an int goes past its largest value?
It wraps round to the most negative value, with no error. 2147483647 plus 1 gives -2147483648.
Can a boolean hold 0 or 1, as in C?
No. In Java a boolean holds only true or false.
byte, short, int and long (integers), float and double (decimals), char (characters) and boolean (true or false).
byte 1 byte, short 2, int 4, long 8, float 4, double 8 and char 2 bytes, the same on every computer.
Whole-number literals are int and decimal literals are double by default; long needs L and float needs f.
Characters are stored as Unicode values, so 'A' + 1 is 66 and (char) ('A' + 1) is 'B'.
Instance and static variables get 0, 0.0, the null character or false; local variables must be assigned before use.
A value past its type's range wraps round without an error, so choose a type that is large enough.
One variable of each primitive type, written with the right literal for each; it prints 16 2026 450 8000000000 and then 49.99 3.141592653589793 A true:
public class Literals {
public static void main(String[] args) {
byte age = 16;
short year = 2026;
int marks = 450;
long worldPopulation = 8000000000L; // about 8 billion: too big for int, needs L
float price = 49.99f; // f makes it a float literal
double pi = 3.141592653589793; // a decimal literal is double by default
char grade = 'A'; // single quotes for one character
boolean passed = true; // only true or false
System.out.println(age + " " + year + " " + marks + " " + worldPopulation);
System.out.println(price + " " + pi + " " + grade + " " + passed);
}
}Seven results that surprise beginners: overflow of int and byte, char arithmetic, integer division and floating-point rounding (the lines printed are -2147483648, -128, 66, B, 3, 3.5 and 0.30000000000000004):
public class Surprises {
public static void main(String[] args) {
int big = Integer.MAX_VALUE; // 2147483647
System.out.println(big + 1); // wraps round to the smallest int
byte b = 127;
b++;
System.out.println(b); // wraps round to -128
char ch = 'A';
System.out.println(ch + 1); // 66: the char is used as its code
System.out.println((char) (ch + 1)); // B
System.out.println(7 / 2); // 3: int division drops the fraction
System.out.println(7 / 2.0); // 3.5
System.out.println(0.1 + 0.2); // not exactly 0.3
}
}Instance variables that are never assigned still have values: the defaults 0, 0.0, the null character (code 0) and false:
public class Defaults {
// Instance variables get default values; local variables do not
byte b;
short s;
int i;
long l;
float f;
double d;
char c;
boolean flag;
public static void main(String[] args) {
Defaults obj = new Defaults();
System.out.println(obj.b + " " + obj.s + " " + obj.i + " " + obj.l);
System.out.println(obj.f + " " + obj.d + " " + (int) obj.c + " " + obj.flag);
}
}Byte is one and short is two, int is four, long eight for you; float is four and double eight, char is two, so set them straight.
Eight friends share a hostel. Byte, Short, Int and Long count things; Float and Double measure things with decimals; Char writes one letter at a time; and Boolean answers only yes or no. Long always signs his name with an L, and Float always signs with an f.
Be Sure It's Legal For Developers Coding Better: byte, short, int, long, float, double, char, boolean.
L.F.: Long gets L, Float gets f, the two suffixes you must not forget.
Term
Name Java's eight primitive data types.
Click to flip
Term
What are the size and range of a byte?
Click to flip
Term
Why does float f = 3.5; not compile?
Click to flip
Term
What does System.out.println('A' + 1) print?
Click to flip
Term
What is the default value of a boolean instance variable?
Click to flip
Term
What is Integer.MAX_VALUE + 1?
Click to flip
One of Java's eight built-in types that holds a single simple value directly in its variable.
A fixed value written directly in the program, such as 450, 49.99f, 'A' or true.
The smallest and largest values a data type can hold.
What happens when a value goes past the end of its type's range and wraps round to the other end.
A number with a fractional part, stored in binary as float or double, usually as a close approximation.
The international character code used by Java's char type, in which 'A' is 65 and 'a' is 97.
The value Java gives automatically to instance variables, static variables and array elements that are not assigned.
A class such as Integer, Double or Character that corresponds to 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 stores one simple value directly in its variable.
Primitive data types are the building blocks of every Java program. This lesson gives the size, range and default value of each of the eight types, shows how to write their literals (the L suffix for long, the f suffix for float, single quotes for char), and explains how to choose the right type for a value. It also covers the surprises every programmer meets: integer overflow, integer division, rounding in floating-point numbers and arithmetic on characters.
A primitive data type is built into the Java language, is named by a reserved keyword and holds a single simple value directly in its variable. Java has exactly eight of them, in four groups. The sizes of the numeric and character types (all except boolean) are fixed by the language itself and are the same on every computer, which is one reason a Java program gives the same results wherever it runs.
The four groups are the integer types (byte, short, int, long), the floating-point types (float, double), the character type (char) and the boolean type (boolean). The exact limits can be printed with the constants of the wrapper classes:
public class Ranges {
public static void main(String[] args) {
System.out.println("byte : " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
System.out.println("short : " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
System.out.println("int : " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("long : " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
System.out.println("float : largest value " + Float.MAX_VALUE);
System.out.println("double: largest value " + Double.MAX_VALUE);
System.out.println("char : " + (int) Character.MIN_VALUE + " to " + (int) Character.MAX_VALUE);
}
}Output:
byte : -128 to 127
short : -32768 to 32767
int : -2147483648 to 2147483647
long : -9223372036854775808 to 9223372036854775807
float : largest value 3.4028235E38
double: largest value 1.7976931348623157E308
char : 0 to 65535
The integer types store whole numbers. int is the usual choice. An integer literal such as 450 is an int by default, so a value beyond the int range must be written with the suffix L to make it a long literal: the world's population of about 8 billion is written 8000000000L. (A small l also works but looks like the digit 1, so L is preferred.) byte and short save memory when a program stores very many small numbers, for example in a large array.
The floating-point types store numbers with a fractional part. A decimal literal such as 49.99 is a double by default, so float price = 49.99; does not compile: the compiler will not silently squeeze a double into a smaller float. Writing 49.99f makes it a float literal. Because these numbers are stored in binary, many decimals can only be approximated: 0.1 + 0.2 prints 0.30000000000000004. For this reason, decimal results should be rounded before display and should not be compared with == without care.
A char holds one character in single quotes, such as 'A', '7' or '#'. It uses 16-bit Unicode, so it can also hold letters of many scripts, such as the Devanagari letter अ. Every character is stored as a number: 'A' is 65, 'a' is 97 and '0' is 48. That is why 'A' + 1 gives the int 66, while (char) ('A' + 1) gives 'B'. Special characters are written as escape sequences, such as '\n' (new line) and '\t' (tab).
A boolean holds only true or false, and it is the result of every relational and logical expression, such as marks >= 40. Unlike C, Java does not treat 0 and 1 as false and true, so boolean ok = 1; is a compile-time error.
'\u0000' for char and false for boolean. Local variables get no default value and must be assigned before they are used, or the program will not compile.Integer.MAX_VALUE + 1 gives -2147483648, and a byte holding 127 becomes -128 when it is increased by 1.Primitive variables hold their values directly. Non-primitive types such as String, arrays and classes, covered in the next lesson, hold references to objects instead.