Skip to content
Laviyx Learn

Try

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.

Beginner9 min readUpdated
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#

TypeSizeRange / valuesExample
byte1 byte−128 to 127byte b = 100;
short2 bytes−32,768 to 32,767short s = 3000;
int4 bytesabout ±2.1 billionint n = 42;
long8 bytesabout ±9.2 × 10¹⁸long big = 9_000_000_000L;
float4 bytes~7 significant digitsfloat f = 2.5f;
double8 bytes~15–16 significant digitsdouble d = 3.14159;
char2 bytesone UTF-16 characterchar c = 'A';
boolean–true or falseboolean ok = true;

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:

Java
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);
    }
}
Output
2147483647
-2147483648
9223372036854775807

Integer overflow#

Integer arithmetic in Java does not crash when it goes past the limit; it silently wraps around:

Java
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);
    }
}
Output
-2147483648
31536000000
1471228928

365 * 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#

LiteralType
42int
42Llong (use an uppercase L; a lowercase l looks like 1)
3.14double
3.14ffloat
'x'char
0xFF, 0b1010int in hexadecimal / binary
1_000_000int with underscores for readability

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:

Java
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);
    }
}
Output
0.30000000000000004
false
2.5
0.67

Never 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:

Java
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'));
    }
}
Output
B 65
true q

letter + 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.

Java
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());
    }
}
Output
Bengaluru has 9 letters
BENGALURU

See Strings for much more.

Primitive vs reference types#

Primitive (int, double, …)Reference (String, arrays, objects)
Variable holdsThe value itselfA reference to an object on the heap
Default for fields0, 0.0, false, '\u0000'null
Can be nullNoYes
Compare values with==.equals()

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
Java
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);
    }
}
Output
3000000000
20.0

Casting users to long before multiplying avoids overflow: 3 billion is larger than Integer.MAX_VALUE.

Key takeaway#

Finished this lesson?