JAVA - Zero to Hero / Java Fundamentals

Operators in JAVA

Operators are symbols used to perform operations on variables and values.

Java supports several types of operators grouped by function.

🔢 Arithmetic Operators

Used to perform basic mathematical operations like addition, subtraction, etc.

Operator

Meaning

Example

+

Addition

a + b

-

Subtraction

a - b

*

Multiplication

a * b

/

Division

a / b

%

Modulus (remainder)

a % b

🔸 Code:

int a = 15, b = 4;
System.out.println("The value of a = " + a);
System.out.println("The value of b = " + b);
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
The value of a = 15
The value of b = 4
a + b = 19
a - b = 11
a * b = 60
a / b = 3
a % b = 3

🔍 Why use them?

To perform calculations like billing, scores, measurements, etc.


🟰🟰 Relational (Comparison) Operators

Used to compare two values. Always returns a boolean (true or false).

Operator

Meaning

Example

==

Equal to

x == y

!=

Not equal to

x != y

>

Greater than

x > y

<

Less than

x < y

>=

Greater or equal

x >= y

<=

Less or equal

x <= y


🔸 Code:

int x = 10, y = 20;
System.out.println("The value of x = " + x);
System.out.println("The value of y = " + y);
System.out.println("x == y: " + (x == y));
System.out.println("x != y: " + (x != y));
System.out.println("x > y: " + (x > y));
System.out.println("x < y: " + (x < y));
System.out.println("x >= 10: " + (x >= 10));
System.out.println("y <= 15: " + (y <= 15));
The value of x = 10
The value of y = 20
x == y: false
x != y: true
x > y: false
x < y: true
x >= 10: true
y <= 15: false

🔍 Why use them?

Used in decisions and conditions, like checking age, passwords, values, etc.


❕Logical Operators

Used to combine boolean expressions. Commonly used in conditions and loops.

Operator

Meaning

Example

&&

Logical AND

x > 10 && y < 20

!

Logical NOT

!true → false

`

`

🔸 Code:

int age = 25;
boolean hasID = true;
System.out.println("Can enter? " + (age >= 18 && hasID));
System.out.println("Underage or no ID? " + (age < 18 || !hasID));
Can enter? true
Underage or no ID? false

🔍 Why use them?

Used in if-else conditions, loops, filters, etc.


🟰 Assignment Operators

Used to assign values to variables.

Operator

Meaning

Example

=

Assign

x = 5

+=

Add and assign

x += 3 → x = x + 3

-=

Subtract and assign

x -= 2

*=

Multiply and assign

x *= 4

/=

Divide and assign

x /= 2

%=

Modulo and assign

x %= 3

🔸 Code:

public class LogicalOperatorsDemo {
    public static void main(String[] args) {
        int age = 20;
        boolean hasID = true;
        boolean isMember = false;

        // Logical AND: Only true if both conditions are true
        boolean canEnter = (age >= 18 && hasID);
        // true && true → true

        // Logical OR: True if at least one condition is true
        boolean getsDiscount = (age < 18 || isMember);
        // false || false → false

        // Logical NOT: Reverses the result
        boolean noID = !hasID;
        // !true → false

        // Output results
        System.out.println("Age: " + age);
        System.out.println("Has ID: " + hasID);
        System.out.println("Is a member: " + isMember);
        System.out.println("Can enter: " + canEnter);        // true
        System.out.println("Gets discount: " + getsDiscount); // false
        System.out.println("Doesn't have ID: " + noID);       // false
    }
}
Age: 20
Has ID: true
Is a member: false
Can enter: true
Gets discount: false
Doesn't have ID: false

🔍 Why use them?

They simplify and shorten mathematical updates to variables.


➕ Unary Operators

Operate on only one operand.

Operator

Meaning

Example

+

Unary plus (no effect)

+a

-

Unary minus

-a

++

Increment

a++, ++a

--

Decrement

a--, --a

!

Logical NOT

!true → false

🔸 Code:

public class UnaryOperatorsDemo {
    public static void main(String[] args) {
        int a = 5;
        boolean flag = true;

        System.out.println("Initial value of a: " + a);

        // Unary plus
        System.out.println("Unary plus (+a): " + (+a));   // Output: 5

        // Unary minus
        System.out.println("Unary minus (-a): " + (-a));  // Output: -5

        // Pre-increment
        System.out.println("Pre-increment (++a): " + (++a));  // Output: 6

        // Post-increment
        System.out.println("Post-increment (a++): " + (a++)); // Output: 6 (then a becomes 7)
        System.out.println("Value after post-increment: " + a); // Output: 7

        // Pre-decrement
        System.out.println("Pre-decrement (--a): " + (--a));  // Output: 6

        // Post-decrement
        System.out.println("Post-decrement (a--): " + (a--)); // Output: 6 (then a becomes 5)
        System.out.println("Value after post-decrement: " + a); // Output: 5

        // Logical NOT
        System.out.println("Logical NOT (!flag): " + (!flag)); // Output: false

        // Bitwise complement
        System.out.println("Bitwise complement (~a): " + (~a)); // Output: -6
    }
}
Initial value of a: 5
Unary plus (+a): 5
Unary minus (-a): -5
Pre-increment (++a): 6
Post-increment (a++): 6
Value after post-increment: 7
Pre-decrement (--a): 6
Post-decrement (a--): 6
Value after post-decrement: 5
Logical NOT (!flag): false
Bitwise complement (~a): -6

🔍 Why use them?

Unary operators are used to perform simple, quick operations on a single operand. They’re helpful in writing cleaner, shorter, and more expressive code.