Java Operators in Java: Complete Beginner Guide (With Examples)

Java Operators: Doing Math and Logic in Java

In our previous post, we learned about Java Variables and Data Types and how to store values like numbers, text, and true/false.

Now it’s time to learn how to work with those values. That’s where operators come in.

Operators are special symbols in Java that allow you to perform:


1) Arithmetic Operators

These operators are used for basic math.

Operator Meaning Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (Remainder) a % b

Example


public class ArithmeticOperators {
    public static void main(String[] args) {

        int a = 10;
        int b = 3;

        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
        System.out.println("Remainder: " + (a % b));
    }
}

2) Assignment Operators

Assignment operators are used to assign values to variables.

Operator Meaning Example
= Assign x = 10
+= Add and assign x += 5
-= Subtract and assign x -= 5
*= Multiply and assign x *= 5
/= Divide and assign x /= 5

Example


public class AssignmentOperators {
    public static void main(String[] args) {

        int x = 10;

        x += 5;
        System.out.println("After += : " + x);

        x -= 3;
        System.out.println("After -= : " + x);

        x *= 2;
        System.out.println("After *= : " + x);

        x /= 4;
        System.out.println("After /= : " + x);
    }
}

3) Comparison (Relational) Operators

Comparison operators compare two values and return true or false.

Operator Meaning Example
== Equal to a == b
!= Not equal a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal a >= b
<= Less than or equal a <= b

4) Logical Operators

Logical operators are used to combine conditions.

Operator Meaning Example
&& AND (both must be true) a > b && b > 0
|| OR (at least one must be true) a > b || b > 0
! NOT (reverse the result) !isTrue

5) Increment and Decrement Operators

These operators are used to increase or decrease values by 1.

Operator Meaning Example
++ Increase by 1 number++
-- Decrease by 1 number--

6) Java Operators Example (All in One Program)


public class JavaOperatorsExample {
    public static void main(String[] args) {

        int a = 15;
        int b = 4;

        // Arithmetic
        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));

        // Comparison
        System.out.println("a > b : " + (a > b));
        System.out.println("a == b : " + (a == b));

        // Logical
        boolean result = (a > b) && (b > 0);
        System.out.println("Logical Result: " + result);

        // Increment
        a++;
        System.out.println("After increment a = " + a);
    }
}

Conclusion

Operators are one of the most important parts of Java because they allow you to:

  • ✅ Perform calculations
  • ✅ Compare values
  • ✅ Write conditions
  • ✅ Build logic for programs

In the next post, we will learn about Java Conditional Statements (if, else, switch) and how to make decisions in Java!

Comments

Post a Comment

Popular posts from this blog

Inheritance in Java (Single, Multilevel, Hierarchical)

The Java Blueprint: Your First Step into Software Development

Java Variables and Data Types: Storing Your Data