AI Hints
Home›Java Tutorial›Variables, Primitive Types, int and long
Java · Lesson 12 of 120

int and long

Whole numbers, big and huge.

⏱ 2 min read▶ Video at 15:23🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

An int can hold numbers up to about two billion. That's plenty, most of the time. When it isn't, you need a long.

  • int: about 2 billion
  • Usually enough
  • Otherwise, long

Limits

Integer dot max value is the biggest number an int can hold. Watch what happens when we add one. A long can hold far more, like the number of people on Earth. Underscores make long numbers easy to read. Java ignores them. Run it. The biggest int is just over two billion. Adding one wraps around, to a huge negative number! And the long holds eight point one billion, easily.

Main.javajava
public class Main {
    public static void main(String[] args) {
        int max = Integer.MAX_VALUE;
        System.out.println("Biggest int: " + max);
        System.out.println("One more:    " + (max + 1));

        long people = 8_100_000_000L;
        System.out.println("People on Earth: " + people);
        System.out.println("Biggest long: " + Long.MAX_VALUE);
    }
}
Output
Biggest int: 2147483647
One more:    -2147483648
People on Earth: 8100000000
Biggest long: 9223372036854775807

Overflow

When a number grows past its type's limit, it wraps around, with no error. This is called overflow. Java won't warn you. So, if numbers can get very big, like money in cents, or counts of people, use long.

keyvalue
Overflowwraps around silently
int limit2,147,483,647
long limitabout 9 quintillion
Rulebig numbers → long

Summary

Int holds up to about two billion. Long holds far more, and needs an L. Past the limit, numbers overflow silently. Next, decimals.

Key takeaways
  • int ≈ 2 billion
  • long needs L
  • Overflow wraps
Next up: double and float — numbers with decimals