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:
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 oprations. These operators are like calculator buttons.
+ → Addition
- → Subtraction
* → Multiplication
/ → Division
% → Remainder
Let's write a simple code for it:
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 does not include after decimals)
System.out.println(x % y); // Prints 1 (Remainder when 10 divided by 3)
}
}
OUTPUT:
2. ASSIGNMENT OPERATOR ( = )
This is a single operator that assign certain value to a variable. = Tells the computer to take what is on right side and store it in the left side. With few modifications it can be used in many ways few of them are as follow:
1. Direct Assignment
int a = 5 put 5 in a
a = a + 2; take a ( 5 ), add 2 and store 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 water bottle (content into variable). You can empty the bottle (variable), pour milk in it, replace the milk by cold drink. Variable will contain the latest value assigned to it regardless of what it had before BUT the data type must be same. You can't put "skibidi" (String) in int a (Integer)
3. RELATIONAL OPERATORS (For Comparison)
These compare two values with each other and gives a boolean which is either true or false .
== → Check if value on left is EQUAL to value on right
!= → Check if value on left is UNEQUAL to value on right
> → Check if value on left is GREATER than value on right
< → Check if value on left is SMALLER than value on right
>= → Check if value on left is either GREATER or EQUAL to value on right
<= → Check if value on left is either SMALLER or EQUAL to value on right
Okay let me do it the old way, look here:
== → 2 == 2 return true (Are 2 banana and 2 banana same? Monkey: Yes)
!= → 2 != 3 return true (Are 2 banana and 3 banana NOT same? Monkey: Yes)
> → 2 > 3 return false (Are 2 banana more than 3 banana? Monkey: No)
< → 2 < 3 return true (Are 2 banana less than 3 banana? Monkey: Yes)
>= → 3 >= 3 return true (Are 3 banana either more or equal to 3 banana? Monkey: Yes)
<= → 4 <= 3 return false (Are 4 banana either less or equal to 3 banana? Monkey: No)
Let's write a simple code for it:
public class Main{
public static void main(String[] args) {
int a = 5, b = 7;
System.out.println(a == b); // Prints false
System.out.println(a != b); // Prints true
System.out.println(a > b); // Prints false
System.out.println(a < b); // Prints true
System.out.println(a >= 5); // Prints true
}
}
OUTPUT:
4. LOGICAL OPERATORS
These operators combine boolean values and work on them. They are like the logic gates we studied in our 10th standard. There are three logical operators:
1. && → it is called AND. Return true if both values on left and right are true
2. || → it is called OR. Return true if any value on left or right is true
3. ! → it is called NOT. Turns true into flase and false into true
Let's write a simple code for it... Try it in VS Code
public class Main{
public static void main(String[] args) {
System.out.println(true && true); // Prints true
System.out.println(true && false); // Prints false
System.out.println(false && false); // Prints false
System.out.println(true || true); // Prints true
System.out.println(true || false); // Prints true
System.out.println(false || false); // Prints false
System.out.println( !true ); // Prints false
System.out.println( !false ); // Prints true
}
}
OUTPUT:
5. UNARY OPERATORS
These are shortcuts for mathematical operation of increasing a number by one or decreasing by one. There are two Unary Operators:
++ → Called Increment. Increase a number by 1
-- → Called Decrement. Decrease a number by 1 ++x
int a = 1;
int b = 2;
a++; same as a = a + 1; Therefore a becomes 2
b--; same as b = b - 1; Therefore a becomes 1
But this is not the end, the urany operators can be used in 2 different ways which perform same actions in through different methods. These are post and pre methods.
Let's write a simple code for it:
public class Main{
public static void main(String[] args) {
int a = 5;
int b = 7;
int c = 10;
int d = 12;
System.out.println(++a);
// Prints 6 (increment first, then print, now a = 6 )
System.out.println(b++);
// Prints 7 (print first, then increment, now b = 8 )
System.out.println(--c);
// Prints 9 (decrement first, then print, now c = 9 )
System.out.println(d--);
// Prints 12 (print first, then decrement, now d = 11 )
// Final values after all operations:
System.out.println(a); // Prints 6
System.out.println(b); // Prints 8
System.out.println(c); // Prints 9
System.out.println(d); // Prints 11
}
}
EXTRA: COMMENTS
Stupid computer throw error when we write something that it do 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
/*
Monkey likes apples too
But hates pineapples
*/
CLOSING
That’s it for the 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’ll understand Input in Java.