← Back to all posts

04 Java Fundamentals

Operators in Java

By Dummy for Dummies


Operators

Welcome back, fellow monkeys!

Monkeys aren't the brightest, but computers start off even dumber, they don't know anything until we tell them. Monkey can look at two bananas and just know which one's bigger. A computer can't. To make a computer capable of comparing things, we write code using operators.

Operators are the basic actions every piece of code relies on, things like:

Example Operations
5 is bigger than 4
2 + 8 equals 10
Assign this value 123 to this variable int number

Operators are what run these operations. Each one is a small decision or calculation, and stacking enough of them together is what makes a program actually do something.


1. Arithmetic Operators

These are the operators which perform basic mathematical operations. These operators are like calculator buttons.

OperatorNameExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division10 / 3 = 3 (integer division)
%Remainder (Modulus)10 % 3 = 1
Java
public class Main {
    public static void main(String[] args) {
        int x = 10, y = 3;
        System.out.println(x + y);  // Prints 13
        System.out.println(x - y);  // Prints 7
        System.out.println(x * y);  // Prints 30
        System.out.println(x / y);  // Prints 3 (integer division)
        System.out.println(x % y);  // Prints 1 (remainder)
    }
}
Console Output
13 7 30 3 1

2. Assignment Operator ( = )

This is a single operator that assigns a certain value to a variable. = tells the computer to take what is on the right side and store it in the left side. With few modifications it can be used in many ways:

Think of assignment operator = as pouring water into a water bottle (content into variable). You can empty the bottle, pour milk in it, replace the milk with cold drink. Variable will contain the latest value assigned to it regardless of what it had before, BUT the data type must be the same. You can't put "skibidi" (String) in int a (Integer).


3. Relational Operators (For Comparison)

These compare two values with each other and give a boolean which is either true or false.

OperatorNameExample (Monkey Version)
==Equal to2 == 2 returns true (Are 2 bananas and 2 bananas same? Monkey: Yes)
!=Not equal to2 != 3 returns true (Are 2 bananas and 3 bananas NOT same? Monkey: Yes)
>Greater than2 > 3 returns false (Are 2 bananas more than 3 bananas? Monkey: No)
<Less than2 < 3 returns true (Are 2 bananas less than 3 bananas? Monkey: Yes)
>=Greater than or equal3 >= 3 returns true
<=Less than or equal4 <= 3 returns false
Java
public class Main {
    public static void main(String[] args) {
        int a = 5, b = 7;
        System.out.println(a == b);  // false
        System.out.println(a != b);  // true
        System.out.println(a > b);   // false
        System.out.println(a < b);   // true
        System.out.println(a >= 5); // true
    }
}
Console Output
false true false true true

4. Logical Operators

These operators combine boolean values and work on them. They are like the logic gates we studied in 10th standard. There are three logical operators:

OperatorNameDescription
&&ANDReturns true if BOTH values are true
||ORReturns true if ANY value is true
!NOTTurns true into false, false into true
Java
public class Main {
    public static void main(String[] args) {
        System.out.println(true && true);   // true
        System.out.println(true && false);  // false
        System.out.println(false && false); // false
        System.out.println(true || true);    // true
        System.out.println(true || false);   // true
        System.out.println(false || false);  // false
        System.out.println(!true);         // false
        System.out.println(!false);        // true
    }
}
Console Output
true false false true true false false true

5. Unary Operators

These are shortcuts for mathematical operations of increasing a number by one or decreasing by one. There are two Unary Operators:

But this is not the end, the unary operators can be used in 2 different ways which perform the same action through different methods. These are post and pre methods.

Java
public class Main {
    public static void main(String[] args) {
        int a = 5, b = 7, c = 10, d = 12;

        System.out.println(++a);  // Prints 6 (increment first, then print)
        System.out.println(b++);  // Prints 7 (print first, then increment)
        System.out.println(--c);  // Prints 9 (decrement first, then print)
        System.out.println(d--);  // Prints 12 (print first, then decrement)

        // Final values after all operations
        System.out.println(a);  // 6
        System.out.println(b);  // 8
        System.out.println(c);  // 9
        System.out.println(d);  // 11
    }
}
Console Output
6 7 9 12 6 8 9 11

Extra: Comments

The computer throws an error when it hits something it doesn't recognize. Comments let you write notes in your code that Java skips over entirely. There are two kinds, single line and multi line.

Single line:
Java
// Monkey likes Bananas

The double forward slash tells the computer "ignore everything after this point, on this line."

Multi-line:
Java
/*  
  Monkey likes apples too  
  But hates pineapples  
*/

Closing

That's it for operators in Java. Operators are like small brain cells for programming. They help computers compare, calculate, assign, and make logical decisions. Without them, our programs would just sit there like a monkey staring at bananas. In the next blog, we will understand Input in Java.

← Previous 03 - Variables and Data types in Java Next → 05 - Input in Java