04 Java Fundamentals | Operators in Java | By Dummy for Dummies

Java Operators | Hassan Bukhari
Java Notes

04 Java Fundamentals | Operators in Java | By Dummy for Dummies

Java Beginner Guide Operators Arithmetic Logical

OPERATORS

Welcome back fellow monkeys!

Monkeys are stupid, but computers are more than just stupids, at least we have few brain cells. Monkey knows which banana is bigger (I am really talking about bananas). Computers don't. So in order to make a computer able to tell which banana is bigger we write codes with operators. Without operators codes are meaningless. They are the basic actions each code must carry like:

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

So operators are agents that run operations. Operations are small thinking actions which make a stupid computer a little bit smarter. This is what every line of code will do and make the programs we make smarter and smarter.


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:

  • Direct Assignment: int a = 5; puts 5 in a
  • Update Assignment: a = a + 2; takes a (5), adds 2, stores back into a (now 7)
  • Shortcut Assignment: a += 3; same as a = a + 3;
  • Shortcut Multiplication: a *= 2; same as a = a * 2;
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:

  • ++ → Called Increment. Increases a number by 1
  • -- → Called Decrement. Decreases a number by 1

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.

  • Pre-Increment ++x → First add one more banana, later do whatever you want
  • Post-Increment x++ → First do whatever you want, later add one more banana
  • Pre-Decrement --x → First eat a banana, later do whatever you want
  • Post-Decrement x-- → First do whatever you want, later eat a banana
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

Stupid computer throws an error when we write something that it does not recognize. So to write important notes in code we use comments. It tells the computer to IGNORE the stuff coming ahead. Comments are single-line and multi-line.

Single line:
Java
// Monkey likes Bananas

The double backslash tells the computer "In this line, ignore all the content after double backslash."

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.