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:
Operator | Operation | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 4 * 6 | 24 |
/ | Division | 15 / 4 | 3 |
% | Modulus (Remainder) | 15 % 4 | 3 |
Example usage:
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:
- Parentheses
()
(highest precedence) - Multiplication
*
, Division/
, Modulus%
- Addition
+
, Subtraction-
(lowest precedence)
Operations with the same precedence are evaluated from left to right.
Examples:
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):
int result = 7 / 2; // 3, not 3.5
The modulus operator returns the remainder of division:
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 tomax - 1
Compound Assignment Operators
Shorthand operators combine arithmetic with assignment:
Operator | Example | Equivalent To |
---|---|---|
+= | a += 5 | a = a + 5 |
-= | a -= 3 | a = a - 3 |
*= | a *= 2 | a = a * 2 |
/= | a /= 4 | a = a / 4 |
%= | a %= 3 | a = a % 3 |
These operators are concise and often more efficient.
Increment and Decrement Operators
Unary operators for adding or subtracting 1:
Operator | Name | Effect |
---|---|---|
++var | Pre-increment | Increments then uses value |
var++ | Post-increment | Uses value then increments |
--var | Pre-decrement | Decrements then uses value |
var-- | Post-decrement | Uses value then decrements |
Examples:
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:
- If either operand is
long
, the result islong
. - Otherwise, if either operand is
int
, the result isint
. - Otherwise, if either operand is
short
, the result is promoted toint
. - Otherwise, if either operand is
byte
, the result is promoted toint
.
This automatic promotion can lead to unexpected results:
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
- Integer Division:
5 / 2
equals2
, not2.5
. - Division by Zero:
5 / 0
causesArithmeticException
. - Overflow: Results that exceed type limits wrap around.java
int max = Integer.MAX_VALUE; // 2147483647 int overflow = max + 1; // -2147483648
- Operator Precedence Mistakes: Use parentheses for clarity.
- Mixing Types: Be cautious when mixing integers with floating-point numbers.
Example Code Snippet
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
}
}