Skip to content

2.3. Integer Operations

Integer operations in Java involve performing mathematical computations using whole numbers. Java provides several primitive integer types (byte, short, int, long) and a comprehensive set of operators for calculations. Mastering these operations is essential for programming logic and algorithm development.

Basic Arithmetic Operators

Java supports five fundamental arithmetic operators for integers:

OperatorOperationExampleResult
+Addition5 + 38
-Subtraction7 - 25
*Multiplication4 * 624
/Division15 / 43
%Modulus (Remainder)15 % 43

Example usage:

java
int a = 10;
int b = 3;
int sum = a + b;        // 13
int difference = a - b; // 7
int product = a * b;    // 30
int quotient = a / b;   // 3
int remainder = a % b;  // 1

Operator Precedence

Java adheres to standard mathematical operator precedence:

  1. Parentheses () (highest precedence)
  2. Multiplication *, Division /, Modulus %
  3. Addition +, Subtraction - (lowest precedence)

Operations with the same precedence are evaluated from left to right.

Examples:

java
int result1 = 2 + 3 * 4;    // 14 (3*4 first, then +2)
int result2 = (2 + 3) * 4;  // 20 (parentheses first)
int result3 = 10 - 4 - 2;   // 4 (left to right: 10-4=6, 6-2=4)

Integer Division and Modulus

Integer division truncates the fractional part (rounds toward zero):

java
int result = 7 / 2;  // 3, not 3.5

The modulus operator returns the remainder of division:

java
int remainder = 7 % 2;  // 1

Common uses of modulus:

  • Checking even/odd: number % 2 == 0 (even)
  • Cycling through values: counter % 3 cycles 0, 1, 2
  • Constraining ranges: number % max gives values 0 to max - 1

Compound Assignment Operators

Shorthand operators combine arithmetic with assignment:

OperatorExampleEquivalent To
+=a += 5a = a + 5
-=a -= 3a = a - 3
*=a *= 2a = a * 2
/=a /= 4a = a / 4
%=a %= 3a = a % 3

These operators are concise and often more efficient.

Increment and Decrement Operators

Unary operators for adding or subtracting 1:

OperatorNameEffect
++varPre-incrementIncrements then uses value
var++Post-incrementUses value then increments
--varPre-decrementDecrements then uses value
var--Post-decrementUses value then decrements

Examples:

java
int x = 5;
int y = ++x;  // x becomes 6, y becomes 6

int a = 5;
int b = a++;  // b becomes 5, a becomes 6

int count = 10;
count--;      // count becomes 9

Numeric Type Conversion in Operations

When operating on different integer types, Java uses these rules:

  1. If either operand is long, the result is long.
  2. Otherwise, if either operand is int, the result is int.
  3. Otherwise, if either operand is short, the result is promoted to int.
  4. Otherwise, if either operand is byte, the result is promoted to int.

This automatic promotion can lead to unexpected results:

java
byte a = 10;
byte b = 20;
// byte result = a + b;  // Error! a + b is int
byte result = (byte)(a + b);  // Must cast

Common Pitfalls and Errors

  1. Integer Division: 5 / 2 equals 2, not 2.5.
  2. Division by Zero: 5 / 0 causes ArithmeticException.
  3. Overflow: Results that exceed type limits wrap around.
    java
    int max = Integer.MAX_VALUE;  // 2147483647
    int overflow = max + 1;       // -2147483648
  4. Operator Precedence Mistakes: Use parentheses for clarity.
  5. Mixing Types: Be cautious when mixing integers with floating-point numbers.

Example Code Snippet

java
public class IntegerOperations {
    public static void main(String[] args) {
        // Basic operations
        int x = 15;
        int y = 4;

        System.out.println("x + y = " + (x + y));      // 19
        System.out.println("x - y = " + (x - y));      // 11
        System.out.println("x * y = " + (x * y));      // 60
        System.out.println("x / y = " + (x / y));      // 3
        System.out.println("x % y = " + (x % y));      // 3

        // Compound assignment
        int count = 10;
        count += 5;    // count = 15
        count *= 2;    // count = 30
        count %= 7;    // count = 2

        // Increment/decrement
        int value = 5;
        int result1 = value++ * 2;  // result1 = 10, value = 6
        int result2 = ++value * 2;  // result2 = 14, value = 7

        // Type conversion example
        byte small = 100;
        int medium = 1000;
        long large = small + medium;  // automatic promotion to long
    }
}