Operators are special symbols used to perform operations on variables and values. From simple addition to complex logic evaluation, operators are the engine of your Java code.
10 + 5, the + is the operator and 10 and 5 are the operands.
These are used to perform common mathematical operations. If you know basic math, these will be very easy to understand.
| Operator | Name | Description | Example |
|---|---|---|---|
+ |
Addition | Adds together two values | x + y |
- |
Subtraction | Subtracts one value from another | x - y |
* |
Multiplication | Multiplies two values | x * y |
/ |
Division | Divides one value by another | x / y |
% |
Modulus | Returns the division remainder | x % y |
++ |
Increment | Increases the value by 1 | ++x |
-- |
Decrement | Decreases the value by 1 | --x |
Assignment operators are used to assign values to variables. The most common one is =, but Java offers "Shorthand" versions too.
int x = 10;
x += 5; // Same as x = x + 5
Other compound operators: -=, *=, /=, %=.
These are used to compare two values. The result of a comparison is always a boolean (true or false). These are the heart of decision-making in if-else statements.
== : Equal to (e.g., 5 == 5 is true)!= : Not equal to (e.g., 5 != 3 is true)> : Greater than< : Less than>= : Greater than or equal toLogical operators are used to determine the logic between variables or values. They allow you to combine multiple comparisons.
Returns true if both statements are true.
(x < 5 && x < 10)
Returns true if at least one statement is true.
(x < 5 || x < 4)
Reverse the result, returns false if the result is true.
!(x < 5 && x < 10)
When multiple operators are used in a single expression, Java follows a specific order of execution. This is called Operator Precedence.
() > Increment/Decrement > Multiplicative * / % > Additive + - > Comparison > Logical.
These are used to perform operations on individual bits (0 and 1) of a number. While less common in web apps, they are vital for system-level programming and cryptography.
& (Bitwise AND)| (Bitwise OR)^ (Bitwise XOR)~ (Bitwise Complement)Operators allow you to manipulate your variables and create complex logic. Now that you know how to calculate and compare, let's learn how to handle text in a more advanced way. In the next chapter, we'll explore Java Strings and their powerful methods.
Next: Java Strings →