Java Primitive Data Types: int, double, boolean—Are You Using Them Correctly?

As a strongly typed language, Java requires variables to explicitly declare their data type when defined. The data type determines not only what values a variable can store but also affects operation rules and memory usage. Today, we’ll explore the three most fundamental and commonly used basic data types: int (integers), double (decimals), and boolean (booleans). We’ll uncover their true characteristics and common pitfalls to avoid.

1. int: The “Stage” for Integers

int stores integers without decimal parts, such as ages, exam scores, array indices, etc. It’s the most commonly used integer type in Java, occupying 4 bytes (32 bits). Its value range is determined by its storage method.

1.1 Value Range

As a 32-bit signed integer, int ranges from -2^31 to 2^31 - 1:
- Minimum: -2147483648
- Maximum: 2147483647

In simple terms, int can store integers approximately between -2.1 billion and 2.1 billion. Examples of valid int values: 123, -456, 0.

1.2 Common Use Cases

  • Counting: E.g., int clickCount = 100; (user click statistics)
  • Indexing: E.g., int index = 5; (array/collection subscripts)
  • Age/Score: E.g., int age = 25; or int score = 95;

1.3 Common Pitfalls

  • Overflow Error: Assigning an integer exceeding int’s maximum value causes a compilation error.
    Example: int a = 2147483648; // Fails (2147483648 > 2147483647)
    Solution: Use long for larger integers (e.g., long bigNum = 123456789012L; with L suffix).

  • Implicit Overflow: Even without direct assignment, operations can cause overflow.
    Example:
    int max = 2147483647;
    int result = max + 1; // Result becomes -2147483648 (due to 32-bit signed integer wrap-around)
    Note: For strict overflow avoidance, use long or BigInteger (but start with basic types as a beginner).

2. double: The “Steward” for Decimals

double stores decimal values (floating-point numbers), such as monetary amounts, height, weight, etc. It occupies 8 bytes (64 bits) and has higher precision than float (4 bytes).

2.1 Value Range

double has an extremely large range: approximately ±1.7976931348623157E+308 to ±4.9000000000000000E-324. In short, it can represent values from near-zero to very large numbers (e.g., 1e300), far exceeding int’s range.

2.2 Common Use Cases

  • Monetary Values: double price = 19.99;
  • Height/Weight: double height = 175.5;
  • Temperature: double temp = 36.5;

2.3 Common Pitfalls

  • Precision Issues: double is a binary floating-point type and cannot precisely represent all decimals (e.g., 0.1 is an infinite loop in binary). Direct comparison with == often yields unexpected results.
    Example: System.out.println(0.1 + 0.2); // Outputs 0.30000000000000004 (not 0.3)
    Solution: Use BigDecimal for precise comparisons (e.g., BigDecimal.valueOf(0.1).add(BigDecimal.valueOf(0.2))) or check if the difference is less than a small epsilon (e.g., Math.abs(a - b) < 1e-9).

  • Overflow to Infinity: Exceeding double’s maximum value results in Infinity.
    Example:
    double maxDouble = Double.MAX_VALUE;
    double overflow = maxDouble * 2; // Result is Infinity

3. boolean: The “Switch” for Logic

boolean is a boolean type with only two values: true (true) and false (false). It represents conditions or states, such as “login status” or “task completion.”

3.1 Value Range

Only two values: true and false.

3.2 Common Use Cases

  • Conditionals: if (isLogin) { ... }
  • Loop Control: while (isRunning) { ... }
  • State Flags: boolean isOpen = false; (e.g., “door open” status)

3.3 Common Pitfalls

  • Assignment Error: boolean variables can only be assigned true or false; using 1 or 0 (even if tolerated by some compilers) is type-mismatched and incorrect.
    Example: boolean flag = 1; // Error! Should be boolean flag = true;

  • Arithmetic Forbidden: boolean cannot participate in arithmetic operations.
    Example: int a = 1 + true; // Compilation error (boolean cannot be added to integers).

Summary: Choose the Right Type to Avoid “Crashes”

  • int: For integers; watch out for range limits (≤2.1 billion) and overflow.
  • double: For decimals; avoid direct == comparisons; use BigDecimal for precision.
  • boolean: For true/false; only assign true/false; never use 1/0.

There’s no “right/wrong” for basic types—choose based on requirements: use int for ages, double for amounts, and boolean for flags. Remember: type consistency is the foundation of correct code. Practice and learn from mistakes to master these types!

Xiaoye