HOME HTML EDITOR C JAVA PHP

Java Operators: Performing Actions

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.

Terminology: In the expression 10 + 5, the + is the operator and 10 and 5 are the operands.

1. Arithmetic Operators

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
int a = 10; int b = 3; System.out.println(a % b); // Output: 1 (Remainder of 10/3)

2. Assignment Operators

Assignment operators are used to assign values to variables. The most common one is =, but Java offers "Shorthand" versions too.

Standard Assignment
int x = 10;
Compound Assignment
x += 5; // Same as x = x + 5

Other compound operators: -=, *=, /=, %=.

3. Comparison (Relational) 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.

4. Logical Operators

Logical operators are used to determine the logic between variables or values. They allow you to combine multiple comparisons.

Logical AND (&&)

Returns true if both statements are true.

(x < 5 && x < 10)
Logical OR (||)

Returns true if at least one statement is true.

(x < 5 || x < 4)
Logical NOT (!)

Reverse the result, returns false if the result is true.

!(x < 5 && x < 10)
[Image of a truth table for AND OR and NOT logical operators]

5. Operator Precedence (BODMAS for Java)

When multiple operators are used in a single expression, Java follows a specific order of execution. This is called Operator Precedence.

Order: Parentheses () > Increment/Decrement > Multiplicative * / % > Additive + - > Comparison > Logical.
int result = 10 + 5 * 2; System.out.println(result); // Output: 20 (Multiplication happens first) int result2 = (10 + 5) * 2; System.out.println(result2); // Output: 30 (Parentheses happen first)

6. Bitwise Operators

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.

Logic is Set!

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 →