Fundamental Arithmetic Operators
Java provides standard arithmetic operations for numerical computations. The basic binary operators include addition (+), subtraction (-), multiplication (*), and division (/). These operators work with two operands, hence the term "binary."
public class OperatorDemo {
public static void main(String[] args) {
int x = 15;
int y = 4;
// Basic arithmetic operations
System.out.println(x + y); // 19
System.out.println(x - y); // 11
System.out.println(x * y); // 60
System.out.println(x / y); // 3 (integer division truncates)
System.out.println(x / (double) y); // 3.75 (explicit casting yields floating-point result)
// Increment and decrement operators
int counter = 5;
int result1 = counter++; // Post-increment: assigns first, then increments
int result2 = ++counter; // Pre-increment: increments first, then assigns
System.out.println(counter); // 7
System.out.println(result1); // 5
System.out.println(result2); // 7
// Decrement demonstration
int num = 8;
int result3 = num--; // Post-decrement
int result4 = --num; // Pre-decrement
System.out.println(num); // 6
System.out.println(result3); // 8
System.out.println(result4); // 6
// Mathematical utility class
System.out.println(Math.pow(2, 4)); // 16.0 (exponentiation returns double)
}
}
Logical Operators and Short-Circuit Evaluation
Logical operators perform boolean algebra: && (AND), || (OR), and ! (NOT). These operators form compound conditions and support short-circuit behavior for optimization.
public class LogicOperatorDemo {
public static void main(String[] args) {
boolean flag1 = true;
boolean flag2 = false;
System.out.println(flag1 && flag2); // false (both must be true)
System.out.println(flag1 || flag2); // true (at least one true)
System.out.println(!(flag1 && flag2)); // true (negation)
// Short-circuit evaluation demonstration
int value = 10;
boolean outcome = (value < 5) && (value++ > 2);
System.out.println(outcome); // false
System.out.println(value); // 10 (unchanged because second condition was skipped)
// The AND operator skips evaluating the second operand when the first is false,
// preventing unnecessary computations and potential side effects.
}
}
Bitwise Manipulation
Bitwise operators directly manipulate individual bits in integer types, offering high-performance calculations. These include AND (&), OR (|), XOR (^), NOT (~), and shift operators (<<, >>).
public class BitwiseDemo {
public static void main(String[] args) {
int valA = 0b00101101; // 45 in decimal
int valB = 0b00001111; // 15 in decimal
// Bitwise AND: 1 only where both bits are 1
System.out.println(valA & valB); // 13 (0b00001101)
// Bitwise OR: 1 where either bit is 1
System.out.println(valA | valB); // 47 (0b00101111)
// Bitwise XOR: 1 where bits differ
System.out.println(valA ^ valB); // 34 (0b00100010)
// Bitwise NOT: inverts all bits
System.out.println(~valB); // -16 (0b11110000 in two's complement)
// Left shift: multiplies by 2^n efficiently
System.out.println(3 << 2); // 12 (0b11 becomes 0b1100)
// Right shift: divides by 2^n efficiently
System.out.println(16 >> 2); // 4 (0b10000 becomes 0b100)
// Bitwise operations execute at the processor level, providing optimal performance.
}
}
Compound Assignment and String Concatenation
Compound assignment operators combine arithmetic with assignment: +=, -=, *=, /=. The + operator also serves as a string concatenator when either operand is a String.
public class AssignmentDemo {
public static void main(String[] args) {
int total = 12;
int addend = 8;
// Compound assignment operations
System.out.println(total += addend); // 20 (equivalent to total = total + addend)
System.out.println(total -= addend); // 12 (equivalent to total = total - addend)
// String concatenation behavior
int alpha = 7;
int beta = 3;
System.out.println(alpha + beta); // 10 (numeric addition)
System.out.println("" + alpha + beta); // "73" (empty string triggers concatenation)
System.out.println(alpha + beta + "" + alpha + beta); // "1073" (evaluation left-to-right)
System.out.println((alpha + beta) + "*" + (alpha + beta)); // "10*10" (parentheses control precedence)
}
}
Ternary Conditional Operator
The ternary operator ? : provides a compact syntax for conditional assignments. It evaluates a boolean expresion and returns one of two values based on the result.
public class TernaryDemo {
public static void main(String[] args) {
int points = 75;
String status = points > 90 ? "Excellent" : "Satisfactory";
// Syntax: condition ? valueIfTrue : valueIfFalse
System.out.println(status); // Satisfactory
}
}
Essential Takeaways
- Master prefix versus postfix increment/decrement behavior—position determines whether the operation executes before or after value retrieval.
- Leverage
Mathclass utilities for advanced mathematical functions beyond basic operators. - Understand short-circuit evaluation to write efficient conditional logic and avoid unnecessary computations.
- Apply bitwise operations for performance-critical scenarios involving flags, masks, or low-level data manipulation.
- Recognize string concatenation rules: the presence of a String operand converts subsequent numeric operations to textual concatenation, while parentheses can enforce arithmetic evaluation.