Operators In Java

I am a passionate software engineer in training at ALX. I have a strong preference for back-end development. I am on course to share my learning journey with you.
Introduction
In our previous discussion, we explored Java's ecosystem, set up our development environment, and executed our first program. Building on our understanding of data types and variables, we now explore operators, which are essential for performing computations and logical operations in Java. Operators allow us to perform various operations on variables, from basic arithmetic to complex logical evaluations.
Operators
Operators are symbols used to perform mathematical or logical operations on variables and values. The main classes of operators in Java are Arithmetic, Relational, Logical and Bitwise operators
Arithmetic Operators
These are used to perform mathematical operations. Java provides the following arithmetic operators:
| Operator | Name | Examples |
| + | Addition (Unary plus) | a + b |
| - | Subtraction (Unary minus) | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
| ++ | Increment | a++ or ++a |
| -- | Decrement | a-- or --a |
Note: Integer division truncates the remainder. To obtain the remainder, use the modulus operator (%).
Example:
public class ArithmeticOperators {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("Sum: " + (a + b));
System.out.println("Difference: " + (a - b));
System.out.println("Product: " + (a * b));
System.out.println("Quotient: " + (a / b));
System.out.println("Remainder: " + (a % b));
}
}
/**
* Output:
* Sum: 13
* Difference: 7
* Product: 30
* Quotient: 3
* Remainder: 1
*/
Relational Operators
Relational operators are used to compare two values and return a boolean result (true or false).
| Operator | Name | Example |
| \== | Equal to | a == b |
| != | Not equal | a != b |
| \> | Greater than | a > b |
| < | Less than | a < b |
| <= | Greater than or equal to | a >= b |
| <= | Less than or equal to | b <= b |
Example:
public class RelationalOperators {
public static void main(String[] args) {
int x = 10, y = 5;
System.out.println("x == y: " + (x == y));
System.out.println("x != y: " + (x != y));
System.out.println("x > y: " + (x > y));
System.out.println("x < y: " + (x < y));
}
}
/**
* Output:
* x == y: false
* x != y: true
* x > y: true
* x < y: false
*/
Logical Operators
Logical operators connect two or more expressions and return a boolean result.
| Operator | Name | Example | ||||
| && | Logical AND | a && b | ||||
| Logical OR | a | b | ||||
| ! | Logical NOT | !a |
Example:
public class LogicalOperators {
public static void main(String[] args) {
boolean A = true, B = false;
System.out.println("A && B: " + (A && B));
System.out.println("A || B: " + (A || B));
System.out.println("!A: " + (!A));
}
}
/**
* Output:
* A && B: false
* A || B: true
* !A: false
*/
Bitwise Operators
Bitwise operators perform operations at the binary level and are commonly used in low-level programming.
| Operator | Name | Example | ||
| & | Bitwise AND | a & b | ||
| ^ | Bitwise XOR | a ^ b | ||
| Bitwise OR | a | b | ||
| ~ | Bitwise Complement | ~a | ||
| << | Left Shift | a << 2 | ||
| \>> | Right Shift | a >> 2 |
Example:
public class BitwiseOperators {
public static void main(String[] args) {
int a = 5, b = 3; // 5 = 0101, 3 = 0011 (binary)
System.out.println("a & b: " + (a & b));
System.out.println("a | b: " + (a | b));
System.out.println("a ^ b: " + (a ^ b));
System.out.println("~a: " + (~a));
System.out.println("a << 1: " + (a << 1));
System.out.println("a >> 1: " + (a >> 1));
}
}
/**
* Output:
* a & b: 1 (0001)
* a | b: 7 (0111)
* a ^ b: 6 (0110)
* ~a: -6 (inverts bits)
* a << 1: 10 (shifts left)
* a >> 1: 2 (shifts right)
*/
Operator Precedence
Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first.
Precedence Table
| Precedence | Operators | Categories | ||
| 1 (Highest) | (), [] | Parenthesis | ||
| 2 | ++, --, ~, ! | Unary | ||
| 3 | *, /, % | Multiplication, Division, Modulus | ||
| 4 | +, - | Addition, Subtraction | ||
| 5 | <<, >>, >>> | Bitwise Shift | ||
| 6 | <, <=, >, >= | Relational | ||
| 7 | instanceof | Type comparison | ||
| 8 | \==, != | Equality | ||
| 9 | & | Bitwise AND | ||
| 10 | ^ | Bitwise XOR | ||
| 11 | Bitwise OR | |||
| 12 | && | Logical AND | ||
| 13 | Logical OR | |||
| 14 | ?: | Ternary | ||
| 15 (Lowest) | \=, +=, -=, *=, /=, %= | Assignment |
Example:
public class OperatorPrecedence {
public static void main(String[] args) {
int result = 10 + 5 * 2; // Multiplication (*) is evaluated before addition (+)
System.out.println("10 + 5 * 2 = " + result);
result = (10 + 5) * 2; // Parentheses change precedence
System.out.println("(10 + 5) * 2 = " + result);
}
}
/**
* Output:
* 10 + 5 * 2 = 20
* (10 + 5) * 2 = 30
*/
Conclusion
Java operators enable calculations, comparisons, and logical operations. Understanding operator precedence ensures correct expression evaluation and prevents unexpected results. Mastering these fundamentals will help you write cleaner, more efficient code, forming a strong foundation for advanced programming.




