Integer primitive types and ranges
What you will learn
Java has four signed integer primitive types: byte, short, int and long. Their widths are 8, 16, 32 and 64 bits. Choose int for ordinary counts and long when the required range exceeds int.
public class Main { public static void main(String[] args) { int orders = 1200; long archiveBytes = 3_000_000_000L; System.out.println(orders); System.out.println(archiveBytes); }}Expected output:
12003000000000The L suffix allows the larger literal to be a long.
How it works
Signed two’s-complement ranges are byte −128 to 127, short −32768 to 32767, int −2147483648 to 2147483647, and long −9223372036854775808 to 9223372036854775807. Integer literals normally have type int when written as ordinary decimal literals; add L for long values. Integer overflow does not automatically raise an exception for ordinary addition.
public class Main { public static void main(String[] args) { int largest = 2147483647; System.out.println(largest + 1); }}Expected output:
-2147483648The int addition wraps to the minimum int value.
Common mistakes
Assigning 3_000_000_000 without L to a long does not repair the oversized int literal. Use 3_000_000_000L. Later, use checked arithmetic when overflow must be detected.
Try it yourself
Choose types for a 100-item count and a 5-billion-byte file size. Print both using valid literals.
These are local Java practice tasks, not automatically graded ByteMind exercises. Use a patched JDK; no network or third-party dependency is needed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Integer primitive types and ranges — knowledge check
Check your understanding of integer primitive types and ranges.
Which declaration stores three billion?
What happens when an int holding 2147483647 is incremented by 1?