Java course menu
Java Data Types: Primitive Types, String and Reference Types
Learn Java's eight primitive types (byte, short, int, long, float, double, char, boolean), their sizes and ranges, integer overflow, floating-point precision, char and Unicode, String, and the difference between primitive and reference types.
On this page
Every value in Java has a type, which decides what values are allowed, how much memory is used, and which operations are possible. Java has two families of types: eight built-in primitive types, and reference types such as String, arrays and every class.
The eight primitive types#
Unlike C and C++, these sizes are the same on every platform. In everyday code, use int for whole numbers, long for very large ones, double for decimals, and boolean for yes/no values.
The limits are available as constants:
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
}
}2147483647
-2147483648
9223372036854775807Integer overflow#
Integer arithmetic in Java does not crash when it goes past the limit; it silently wraps around:
public class Main {
public static void main(String[] args) {
int max = Integer.MAX_VALUE;
System.out.println(max + 1);
int msPerDay = 24 * 60 * 60 * 1000;
long msPerYear = 365L * msPerDay;
int wrong = 365 * msPerDay;
System.out.println(msPerYear);
System.out.println(wrong);
}
}-2147483648
31536000000
1471228928365 * msPerDay multiplies two ints and overflows before the result is stored. Writing 365L makes the multiplication happen in long. If you want an error instead of silent wrapping, use Math.multiplyExact and Math.addExact, which throw ArithmeticException on overflow.
Literals and suffixes#
Floating point: double and float#
double is the default for decimals. Like most languages, Java stores decimals in binary, so many values are not exact:
public class Main {
public static void main(String[] args) {
double sum = 0.1 + 0.2;
System.out.println(sum);
System.out.println(sum == 0.3);
System.out.println(10.0 / 4);
System.out.printf("%.2f%n", 2.0 / 3);
}
}0.30000000000000004
false
2.5
0.67Never compare doubles with ==; check whether they are close (Math.abs(a - b) < 1e-9). For money, use whole paise in a long, or java.math.BigDecimal, which stores exact decimal values.
char and Unicode#
A char holds one UTF-16 code unit, which covers most characters in use. Underneath it is a number, so you can do arithmetic with it:
public class Main {
public static void main(String[] args) {
char letter = 'A';
char next = (char) (letter + 1);
int code = letter;
System.out.println(next + " " + code);
System.out.println(Character.isDigit('7') + " " + Character.toLowerCase('Q'));
}
}B 65
true qletter + 1 produces an int, so it must be cast back to char. The Character class has helpers such as isDigit, isLetter and toUpperCase.
boolean#
A boolean is true or false, the result of every comparison. Unlike C++, Java does not treat numbers as booleans: if (count) does not compile; write if (count != 0).
String#
String is not a primitive type; it is a class in java.lang. Strings are written in double quotes and are immutable: methods like toUpperCase() return a new string.
public class Main {
public static void main(String[] args) {
String city = "Bengaluru";
System.out.println(city + " has " + city.length() + " letters");
System.out.println(city.toUpperCase());
}
}Bengaluru has 9 letters
BENGALURUSee Strings for much more.
Primitive vs reference types#
That last row is important. For reference types, == checks whether two variables refer to the same object, not whether the contents are equal. Always compare strings with .equals(); see Strings.
Common mistakes#
Practice#
Practice exercise
A video platform has 2_500_000 users who each watch 1_200 seconds a day. Print the total seconds watched per day without overflow, and the average watch time in minutes as a decimal with one digit after the point.
Show solution
public class WatchTime {
public static void main(String[] args) {
int users = 2_500_000;
int secondsEach = 1_200;
long totalSeconds = (long) users * secondsEach;
double minutesEach = secondsEach / 60.0;
System.out.println(totalSeconds);
System.out.printf("%.1f%n", minutesEach);
}
}3000000000
20.0Casting users to long before multiplying avoids overflow: 3 billion is larger than Integer.MAX_VALUE.
Key takeaway#
Finished this lesson?