By Dummy for Dummies
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:
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.
These are the operators which perform basic mathematical operations. These operators are like calculator buttons.
| Operator | Name | Example |
|---|---|---|
| + | Addition | 5 + 3 = 8 |
| - | Subtraction | 5 - 3 = 2 |
| * | Multiplication | 5 * 3 = 15 |
| / | Division | 10 / 3 = 3 (integer division) |
| % | Remainder (Modulus) | 10 % 3 = 1 |
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) } }
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:
int a = 5; puts 5 in aa = a + 2; takes a (5), adds 2, stores back into a (now 7)a += 3; same as a = a + 3;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).
These compare two values with each other and give a boolean which is either true or false.
| Operator | Name | Example (Monkey Version) |
|---|---|---|
| == | Equal to | 2 == 2 returns true (Are 2 bananas and 2 bananas same? Monkey: Yes) |
| != | Not equal to | 2 != 3 returns true (Are 2 bananas and 3 bananas NOT same? Monkey: Yes) |
| > | Greater than | 2 > 3 returns false (Are 2 bananas more than 3 bananas? Monkey: No) |
| < | Less than | 2 < 3 returns true (Are 2 bananas less than 3 bananas? Monkey: Yes) |
| >= | Greater than or equal | 3 >= 3 returns true |
| <= | Less than or equal | 4 <= 3 returns false |
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 } }
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:
| Operator | Name | Description |
|---|---|---|
| && | AND | Returns true if BOTH values are true |
| || | OR | Returns true if ANY value is true |
| ! | NOT | Turns true into false, false into true |
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 } }
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.
++x → First add one more banana, later do whatever you wantx++ → First do whatever you want, later add one more banana--x → First eat a banana, later do whatever you wantx-- → First do whatever you want, later eat a bananapublic 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 } }
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.
// Monkey likes Bananas
The double forward slash tells the computer "ignore everything after this point, on this line."
/*
Monkey likes apples too
But hates pineapples
*/
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.