1. Operators Overview
An operator is a symbol that performs a particular operation on one or more operands.
| Operator Category | Operators |
|---|---|
| Arithmetic Operators | + - * / % |
| Relational Operators | < <= > >= == != |
| Logical Operators | && || ! |
| Unary Operators | Increment, decrement |
| New | new |
| instanceof | instanceof |
| Ternary | ?: |
| Bitwise | >> << & | ^ ~ |
| Assignment | = |
| Member Access | . [] () type |
2. Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.
Assume integer variable A = 10 and variable B = 20.
| Operator | Description | Example |
|---|---|---|
| + Addition | Adds values on either side of the operator. | A + B gives 30 |
| - Subtraction | Subtracts the right-hand operand from the left-hand operand. | A - B gives -10 |
| * Multiplication | Multiplies values on either side of the operator. | A * B gives 200 |
| / Division | Divides the left operand by the right operand. | B / A gives 2 |
| % Modulus | Divides and returns the remainder. | B % A gives 0 |
| ++ Increment | Increases the value of the operand by 1. | B++ gives 21 |
| -- Decrement | Decreases the value of the operand by 1. | B-- gives 19 |
3. Relational Operators
Relational operators compare two values and produce a boolean result.
Assume variable A = 10 and variable B = 20.
| Operator | Description | Example |
|---|---|---|
== | Checks whether the values of two operands are equal. | (A == B) is not true |
!= | Checks whether the values of two operands are not equal. | (A != B) is true |
> | Checks whether the left operand is greater than the right operand. | (A > B) is not true |
< | Checks whether the left operand is less than the right operand. | (A < B) is true |
>= | Checks whether the left operand is greater than or equal to the right operand. | (A >= B) is not true |
<= | Checks whether the left operand is less than or equal to the right operand. | (A <= B) is true |
4. Bitwise Operators
Java defines bitwise operators that can be applied to integer types such as long, int, short, char and byte.
Bitwise operators work on individual bits and perform bit-by-bit operations.
Binary Example
a = 60 = 0011 1100 b = 13 = 0000 1101 ------------------- a & b = 0000 1100 a | b = 0011 1101 a ^ b = 0011 0001 ~a = 1100 0011
| Operator | Description | Example |
|---|---|---|
& | Binary AND copies a bit if it exists in both operands. | A & B gives 12 |
| | Binary OR copies a bit if it exists in either operand. | A | B gives 61 |
^ | Binary XOR copies the bit if it is set in one operand but not both. | A ^ B gives 49 |
~ | Binary ones complement; flips the bits. | ~A gives -61 |
<< | Left shift moves the left operand left by the specified number of bits. | A << 2 gives 240 |
>> | Right shift moves the left operand right by the specified number of bits. | A >> 2 gives 15 |
>>> | Zero-fill right shift moves bits right and fills shifted positions with zeros. | A >>> 2 gives 15 |
5. Logical Operators
Assume Boolean variable A = true and variable B = false.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND. The condition is true when both operands are true. | (A && B) is false |
|| | Logical OR. The condition is true when at least one operand is true. | (A || B) is true |
! | Logical NOT. Reverses the logical state of its operand. | !(A && B) is true |
6. Assignment Operators
Assignment operators assign values to variables and can combine assignment with arithmetic or bitwise operations.
| Operator | Description | Equivalent Form |
|---|---|---|
= | Simple assignment operator. | C = A + B |
+= | Add AND assignment. | C = C + A |
-= | Subtract AND assignment. | C = C - A |
*= | Multiply AND assignment. | C = C * A |
/= | Divide AND assignment. | C = C / A |
%= | Modulus AND assignment. | C = C % A |
<<= | Left shift AND assignment. | C = C << 2 |
>>= | Right shift AND assignment. | C = C >> 2 |
&= | Bitwise AND assignment. | C = C & 2 |
^= | Bitwise exclusive OR assignment. | C = C ^ 2 |
|= | Bitwise inclusive OR assignment. | C = C | 2 |
7. Miscellaneous Operators
The source identifies two important miscellaneous operators:
Conditional / Ternary
?:
Used to evaluate a Boolean expression and choose one of two values.
instanceof
instanceof
Checks whether an object reference refers to an instance of a particular class or interface type.
8. Conditional Operator (?:)
The conditional operator is also known as the ternary operator. It consists of three operands and is used to evaluate Boolean expressions.
variable = (expression) ? value if true : value if false;
Example
public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20 : 30;
System.out.println("Value of b is : " + b);
b = (a == 10) ? 20 : 30;
System.out.println("Value of b is : " + b);
}
}
Output
Value of b is : 30 Value of b is : 20
9. instanceof Operator
The instanceof operator is used with object reference variables. It checks whether the object is of a particular class type or interface type.
(Object reference variable) instanceof (class/interface type)
If the object referred to by the variable on the left passes the IS-A check for the class or interface on the right, the result is true.
Example
public class Test {
public static void main(String args[]) {
String name = "James";
// name is of type String
boolean result = name instanceof String;
System.out.println(result);
}
}
Output
true
10. Precedence of Java Operators
Operator precedence determines the grouping of terms in an expression and affects how the expression is evaluated.
Example
x = 7 + 3 * 2;
x becomes 13, not 20, because multiplication has higher precedence than addition.
| Category | Operator | Associativity |
|---|---|---|
| Postfix | expression++ expression-- | Left to right |
| Unary | ++expression --expression +expression -expression ~ ! | 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 | ?: | Right to left |
| Assignment | = += -= *= /= %= ^= |= <<= >>= >>>= | Right to left |
11. Data Structure
The source describes a data structure as a kind of user-defined data type and as a way of storing data in a computer so that it can be used efficiently.
Linear Data Structure
- Arrays
- Stacks
- Linked List
- Doubly Linked List
- Structs
- Enumerators
- HashCode
- HashTable
- Unions
- Queue
- DQueue
- Priority Queue
Non-Linear Data Structure
- Trees
- Graphs
12. Quick Revision
| Topic | Key Point |
|---|---|
| Arithmetic | + - * / % ++ -- |
| Relational | < <= > >= == != |
| Logical | && || ! |
| Bitwise | & | ^ ~ << >> >>> |
| Assignment | = += -= *= /= %= <<= >>= &= ^= |= |
| Ternary | condition ? valueIfTrue : valueIfFalse |
| instanceof | Checks whether an object is an instance of a class or interface. |
| Precedence | Determines the order in which operators are evaluated. |
| Linear Data Structure | Arrays, stacks, linked lists, queues, priority queues and others listed in the source. |
| Non-Linear Data Structure | Trees and graphs. |