04 Java Fundamentals | Operators in Java | By Dummy for Dummies
04 Java Fundamentals | Operators in Java | By Dummy for Dummies
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:
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.
| 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) } }
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 asa = a + 3; - Shortcut Multiplication:
a *= 2;same asa = a * 2;
3. RELATIONAL OPERATORS (For Comparison)
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 } }
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:
| 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 } }
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
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 } }
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.
// Monkey likes Bananas
The double backslash tells the computer "In this line, ignore all the content after double backslash."
/*
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.