Introduction to Java Operators
In Java, operators are special symbols that perform certain operations on a single, two, or three operands and then produce a final result. Operators in Java are the building blocks of any logical operations within a Java program. Java offers a wide range of operators for efficient data management, from simple arithmetic to complex bitwise operations. Explore our Java course syllabus to get started.
1. Arithmetic Operators in Java
Arithmetic operators are used in mathematical statements in the same manner as in algebra. They are the most frequently used Java operators for simple data management.
| Operator | Name | Description | Example (A=10,B=20) |
| + | Addition | Adds values on either side of the operator | A+B=30 |
| – | Subtraction | Subtracts the right-hand operand from the left-hand operand | A−B=−10 |
| * | Multiplication | Multiplies values on either side of the operator | A∗B=200 |
| / | Division | Divides the left-hand operand by the right-hand operand | B/A=2 |
| % | Modulus | Divides the left-hand operand by the right-hand operand and returns the remainder | B%A=0 |
| ++ | Increment | Increases the value of the operand by 1 | B++=21 |
| — | Decrement | Decreases the value of the operand by 1 | B−−=19 |
Post-increment versus Pre-increment
The most common confusion is the placement of the operators (the placement of the ++ or the — operator).
- Prefix (++count): The operator increments the variable and then uses the new value for the expression.
- Postfix (count++): The operator uses the current value for the expression and then increments the variable.
Code Example:
int x = 10;
int y = ++x; // x is 11, y is 11
int a = 10;
int b = a++; // a is 11, b is 10
2. Relational (Comparison) Operators in Java
Relational operators check the relationship between the two operands, i.e., the relationship of the first operand to the second operand. The relational operators check for equality and order between the two operands. The outcome of a relational operator is always a Boolean data type (true or false).
| Operator | Description | Example (A=10,B=20) |
| == | Checks if the values of two operands are equal | (A==B) is false |
| != | Checks if the values of two operands are not equal | (A!=B) is true |
| > | Checks if the left operand is greater than the right operand | (A>B) is false |
| < | Checks if the left operand is less than the right operand | (A<B) is true |
| >= | Checks if the left operand is greater than or equal to the right operand | (A>=B) is false |
Begin your learning journey with our Java tutorial for beginners.
3. Logical Operators in Java
The logical operators are mainly used to create complex conditions using several relational expressions. They are subject to the rules of Boolean algebra.
- Logical AND (&&): This operator returns true if both operands are true. This operator also uses “short-circuit” evaluation, meaning that if the first operand is false, the second operand is not evaluated.
- Logical OR (||): This operator returns true if at least one of the operands is true. If the first operand is true, the second operand is not evaluated.
- Logical NOT (!): This operator simply reverses the logical condition of the operand.
Code Example:
boolean isAdult = true;
boolean hasTicket = false;
if (isAdult && hasTicket) {
System.out.println(“Entry Allowed”);
} else if (!hasTicket) {
System.out.println(“Please buy a ticket.”);
}
4. Bitwise Operators in Java
Java has several bitwise operators, and these are applicable to the integer data types: long, int, short, char, and byte.
| Operator | Name | Description |
| & | Bitwise AND | Result is 1 if both bits are 1 |
| | | Bitwise OR | Result is 1 if either bit is 1 |
| ^ | Bitwise XOR | Result is 1 if bits are different |
| ~ | Bitwise Complement | Inverts all bits (unary operator) |
| << | Left Shift | Shifts bits to the left, fills with zero |
| >> | Right Shift | Shifts bits to the right, fills with sign bit |
| >>> | Unsigned Right Shift | Shifts bits to the right, fills with zero |
5. Assignment Operators in Java
The assignment operators are used for assignment operations. The simple assignment operator is =, but there are compound assignment operators that can be used to shorten the code for certain operations.
| Operator | Example | Equivalent to |
|---|---|---|
| = | C = A + B | Assigns the value of A+B to C |
| += | C += A | C = C + A |
| -= | C -= A | C = C – A |
| *= | C *= A | C = C * A |
| /= | C /= A | C = C / A |
| %= | C %= A | C = C % A |
Gain expertise with our Java training in Chennai.
6. The Ternary Operator in Java (?:)
The ternary operator is a special operator in Java, and it is the only operator that takes three operands. The ternary operator is often used to replace an if-else statement.
Syntax: variable = (condition) ? valueIfTrue : valueIfFalse;
Code Example:
int time = 20;
String greeting = (time < 18) ? “Good day” : “Good evening”;
System.out.println(greeting); // Prints “Good evening”
7. The instanceof Operator
The instanceof operator is used only for object reference variables. The instanceof operator checks whether the object is of a particular type or not, i.e., it checks whether it is of class type or interface type.
String name = “SLA”;
boolean result = name instanceof String; // Returns true
Operator Precedence in Java
If an expression contains more than one operator, Java has its own order of precedence. The use of parentheses is allowed.
- Postfix: expression++, expression–
- Unary: ++expression, –expression, +, -, ~, !
- Multiplicative: *, /, %
- Additive: +, –
- Shift: <<, >>, >>>
- Relational: <, >, <=, >=, instanceof
- Equality: ==, !=
- Bitwise AND: &
- Bitwise XOR: ^
- Bitwise OR: |
- Logical AND: &&
- Logical OR: ||
- Ternary: ?:
- Assignment: =, +=, -=, etc.
Thrive in your career with our Java interview questions and answers.
Conclusion
Mastering Java operators enables you to write compact and efficient code. Arithmetic and relational operators are sufficient for day-to-day programming, but learning bitwise and ternary operators helps you optimize your code for better performance. Learn Java operators comprehensively in our software training institute in Chennai.
