Chapter 4: Operators

What are Operators?

Operators are special symbols that tell the computer to perform some action on one or more values (called operands).

Example: 5 + 3 → + is the operator, 5 and 3 are operands, result is 8.

Java has several categories of operators. We’ll cover them one by one.

1. Arithmetic Operators (Math Operations)

These are the ones you already know from school.

Operator Meaning Example Result Notes
+ Addition 10 + 5 15 Also used for String concatenation
Subtraction 20 – 8 12
* Multiplication 4 * 6 24
/ Division 10 / 3 3 Integer division → drops decimal
% Modulus (Remainder) 10 % 3 1 Very useful!
+ (unary) Positive (rarely used) +5 5
– (unary) Negation -7 -7

Important Examples:

Java

String Concatenation with +

Java

2. Relational / Comparison Operators (True or False)

These compare two values and return a boolean (true or false).

Operator Meaning Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 10 > 7 true
< Less than 4 < 9 true
>= Greater than or equal 10 >= 10 true
<= Less than or equal 5 <= 3 false

Example:

Java

Warning: For Strings, never use == to compare content! Use .equals() instead.

Java

3. Logical Operators (Combining Conditions)

Used to combine multiple boolean expressions.

Operator Meaning Example Result
&& AND (both true) (age >= 18) && (age <= 60) true/false
|| OR (at least one true) (isRaining) || (isWeekend) true/false
! NOT (reverse) !isRaining true/false

Short-circuit behavior (very important!):

  • && → if first condition is false, second is not evaluated
  • || → if first condition is true, second is not evaluated

Example:

Java

4. Assignment Operators (Putting Values into Variables)

Operator Meaning Example Same as
= Simple assignment x = 10;
+= Add and assign x += 5; x = x + 5;
-= Subtract and assign x -= 3; x = x – 3;
*= Multiply and assign x *= 2; x = x * 2;
/= Divide and assign x /= 4; x = x / 4;
%= Modulus and assign x %= 7; x = x % 7;

Handy Example:

Java

5. Increment / Decrement Operators (Very Common!)

Used to increase or decrease a value by 1.

Operator Meaning Example Effect on x
++x Pre-increment y = ++x; x increases first, then assigned
x++ Post-increment y = x++; assigned first, then x increases
–x Pre-decrement y = –x; x decreases first
x– Post-decrement y = x–; assigned first, then x decreases

Important Examples (this confuses many beginners!):

Java

6. Ternary Operator (Short if-else)

A one-line shortcut for simple if-else.

Syntax:

Java

Example:

Java

Real-life analogy: Ternary is like asking: “Is it raining?” → Yes → take umbrella : No → don’t take.

7. Bitwise Operators (Advanced – for later)

(We’ll just introduce them now – you’ll use them rarely at first)

Operator Meaning Example
& Bitwise AND 5 & 3
Bitwise OR
^ Bitwise XOR 5 ^ 3
~ Bitwise NOT (invert) ~5
<< Left shift 5 << 1
>> Right shift (signed) 5 >> 1

Example (optional for now):

Java

Quick Recap Table (Your Cheat Sheet)

Category Operators Purpose
Arithmetic + – * / % Math calculations
Relational == != > < >= <= Comparisons → boolean
Logical `&&
Assignment = += -= *= /= %= Assign values quickly
Increment/Dec ++ — (pre & post) +1 or -1
Ternary ? : Short if-else

Homework for You (Try These!)

  1. Create a program OperatorsDemo.java
    • Take two numbers (hardcode them)
    • Print sum, difference, product, quotient, remainder
    • Check if first number is greater than second
    • Use ternary to print “Even” or “Odd” for the first number
  2. Play with increment/decrement:
    Java
  3. Write a program that calculates final price after discount using ternary.

You’re doing awesome! Operators are like the verbs of programming — now you know how to make your program do math, compare things, and make decisions.

Ready for Chapter 5: Input and Output (where you’ll start taking input from the user)? Or want me to explain any operator again with more examples? Just say the word! ☕🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *