Java Basic Operators
Java Basic Operators
Section titled “Java Basic Operators”Operators are special symbols that perform operations on one, two, or three operands (variables or values) and return a result. Java provides a rich set of operators grouped as follows:
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- Miscellaneous Operators (Ternary, instanceof)
The Arithmetic Operators
Section titled “The Arithmetic Operators”Used for mathematical calculations.
Assume int a = 10; and int b = 20;
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b gives 30 |
- | Subtraction | a - b gives -10 |
* | Multiplication | a * b gives 200 |
/ | Division (Integer division if both operands are integers) | b / a gives 2. 10 / 3 gives 3. |
% | Modulus (Remainder of division) | b % a gives 0. 10 % 3 gives 1. |
++ | Increment (Prefix ++a or Postfix a++). Increases value by 1. | a = 10; b = ++a; (a is 11, b is 11). a = 10; b = a++; (a is 11, b is 10). |
-- | Decrement (Prefix --a or Postfix a--). Decreases value by 1. | a = 10; b = --a; (a is 9, b is 9). a = 10; b = a--; (a is 9, b is 10). |
The Relational Operators
Section titled “The Relational Operators”Used to compare two values. Result is always a boolean (true or false).
Assume int a = 10; and int b = 20;
| Operator | Description | Example |
|---|---|---|
== | Equal to | (a == b) is false. |
!= | Not equal to | (a != b) is true. |
> | Greater than | (a > b) is false. |
< | Less than | (a < b) is true. |
>= | Greater than or equal to | (a >= b) is false. |
<= | Less than or equal to | (a <= b) is true. |
The Bitwise Operators
Section titled “The Bitwise Operators”Operate directly on the individual bits of integer types (long, int, short, char, byte).
Assume int a = 60; (binary 0011 1100) and int b = 13; (binary 0000 1101)
| Operator | Description | Example |
|---|---|---|
& | Bitwise AND (1 if both bits are 1) | (a & b) gives 12 (binary 0000 1100) |
| | Bitwise OR (1 if at least one bit is 1) | (a | b) gives 61 (binary 0011 1101) |
^ | Bitwise XOR (1 if bits are different) | (a ^ b) gives 49 (binary 0011 0001) |
~ | Bitwise Complement (Unary - flips all bits) | (~a) gives -61 (due to 2’s complement representation) |
<< | Signed Left Shift (Shifts bits left, fills with 0) | a << 2 gives 240 (binary 1111 0000) |
>> | Signed Right Shift (Shifts bits right, fills with sign bit) | a >> 2 gives 15 (binary 0000 1111). (-60) >> 2 would fill with 1s. |
>>> | Unsigned Right Shift (Shifts bits right, fills with 0) | a >>> 2 gives 15 (binary 0000 1111). (-60) >>> 2 fills with 0s, resulting in a large positive number. |
The Logical Operators
Section titled “The Logical Operators”Operate on boolean operands. Often used with relational operators to create complex conditions.
Assume boolean x = true; and boolean y = false;
| Operator | Description | Example |
|---|---|---|
&& | Logical AND (true if both operands are true). Short-circuiting: if the left operand is false, the right is not evaluated. | (x && y) is false. |
|| | Logical OR (true if at least one operand is true). Short-circuiting: if the left operand is true, the right is not evaluated. | (x || y) is true. |
! | Logical NOT (Unary - inverts boolean value) | !x is false. !y is true. |
Note: Java also has bitwise & and | which can be used on boolean operands, but they are not short-circuiting (both sides are always evaluated). Use && and || for logical operations unless you specifically need the non-short-circuiting behavior.
The Assignment Operators
Section titled “The Assignment Operators”Used to assign values to variables.
| Operator | Description | Example (Assume int c = 10, a = 5) |
|---|---|---|
= | Simple Assignment | c = a; (c becomes 5) |
+= | Add and Assign | c += a; is equivalent to c = c + a; (c becomes 15) |
-= | Subtract and Assign | c -= a; is equivalent to c = c - a; (c becomes 5) |
*= | Multiply and Assign | c *= a; is equivalent to c = c * a; (c becomes 50) |
/= | Divide and Assign | c /= a; is equivalent to c = c / a; (c becomes 2) |
%= | Modulus and Assign | c %= a; is equivalent to c = c % a; (c becomes 0) |
<<= | Left Shift and Assign | c <<= 2; is equivalent to c = c << 2; |
>>= | Signed Right Shift and Assign | c >>= 2; is equivalent to c = c >> 2; |
>>>= | Unsigned Right Shift and Assign | c >>>= 2; is equivalent to c = c >>> 2; |
&= | Bitwise AND and Assign | c &= a; is equivalent to c = c & a; |
|= | Bitwise OR and Assign | c |= a; is equivalent to c = c | a; |
^= | Bitwise XOR and Assign | c ^= a; is equivalent to c = c ^ a; |
Miscellaneous Operators
Section titled “Miscellaneous Operators”Conditional Operator (Ternary Operator ? :)
Section titled “Conditional Operator (Ternary Operator ? :)”A shorthand for an if-else statement, often used for simple assignments. Syntax: condition ? value_if_true : value_if_false
Example:
public class TernaryTest { public static void main(String[] args) { int a = 10; int b;
// If (a == 1) is true, assign 20 to b, otherwise assign 30 b = (a == 1) ? 20 : 30; System.out.println("Value of b is: " + b); // Output: 30
// If (a == 10) is true, assign 20 to b, otherwise assign 30 b = (a == 10) ? 20 : 30; System.out.println("Value of b is: " + b); // Output: 20
String status = (a > 5) ? "Active" : "Inactive"; System.out.println("Status: " + status); // Output: Active }}instanceof Operator
Section titled “instanceof Operator”Checks if an object is an instance of a particular class, a subclass, or an implementation of an interface. Returns a boolean.
Syntax: (objectReference instanceof Type)
Example:
public class InstanceofTest { public static void main(String[] args) { String name = "James"; boolean isString = name instanceof String; // true System.out.println("Is name a String? " + isString);
Object obj = name; // Assign String to Object reference boolean isObject = obj instanceof Object; // true System.out.println("Is obj an Object? " + isObject);
boolean isList = name instanceof java.util.List; // false System.out.println("Is name a List? " + isList);
// Check for null String nullStr = null; boolean isNullString = nullStr instanceof String; // false (instanceof returns false for null operand) System.out.println("Is nullStr a String? " + isNullString);
// Inheritance check Vehicle myVehicle = new Car(); boolean isVehicle = myVehicle instanceof Vehicle; // true boolean isCar = myVehicle instanceof Car; // true System.out.println("Is myVehicle a Vehicle? " + isVehicle); System.out.println("Is myVehicle a Car? " + isCar);
// Pattern Matching for instanceof (Java 16+) Object data = "Some Text"; if (data instanceof String s) { // If true, automatically casts 'data' to String 's' System.out.println("String length: " + s.length()); } else { System.out.println("Not a String"); } }}
class Vehicle {}class Car extends Vehicle {}Operator Precedence
Section titled “Operator Precedence”Determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first. If operators have the same precedence, evaluation proceeds based on associativity (usually left-to-right).
Example: x = 7 + 3 * 2; Multiplication (*) has higher precedence than addition (+), so 3 * 2 is evaluated first (6), then 7 + 6 is evaluated (13). x becomes 13.
Use parentheses () to override precedence and force a specific evaluation order. Example: x = (7 + 3) * 2; (evaluates to 20).
Precedence Table (Highest to Lowest):
| Category | Operator | Associativity |
|---|---|---|
| Postfix | expr++ expr-- [] . ()(method call) | Left to right |
| Unary | ++expr --expr +expr -expr ~ ! | Right to left |
| Multiplicative | * / % | Left to right |
| Additive | + - | Left to right |
| Shift | << >> >>> | Left to right |
| Relational | < > <= >= instanceof | Left to right |
| Equality | == != | Left to right |
| Bitwise AND | & | Left to right |
| Bitwise XOR | ^ | Left to right |
| Bitwise OR | | | Left to right |
| Logical AND | && | Left to right |
| Logical OR | || | Left to right |
| Conditional (Ternary) | ? : | Right to left |
| Assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= | Right to left |
What’s Next?
Section titled “What’s Next?”The next chapter explores loop control structures (for, while, do-while) in Java, essential tools for repeating blocks of code.