Core Java(Part-4)

Content
.Operators
.new
.Conditionals loop
Operators
Operator is a symbols in java which tells to compiler to perform some operations. Operator is used to perform operations between operands.
Example | Lame Explanation | |
Arithmetic | + - * / % | Math wale buttons ➕➖✖️➗ |
Assignment | = | “Yeh value uss dabbe mein daal do” |
Comparison | == != > < | “Compare karte hain bhai” |
Logical | `&&, | |
Unary | ++ -- | “Ek zyada ya ek kam” |
Bitwise | `& | ^ << >>` |
Ternary | ? : | Chhoti if-else shortcut |
Assignment operator
Assignment operator used to assign value to the operand
eg:- int a=10;//in a variable store the value 10
Comparison operator
Comparison operator used to compare the values, operand. It gives only output in the boolean format(true/false).
Logical operator
Logical Operators = Java ka dimaag (brain) for TRUE or FALSE thinking.
They help Java decide stuff like:
“Should I do this?”
“Are BOTH conditions true?”
“Is at least ONE true?”
“Is it NOT true?”
| AND | “Both things must be true, bro!” ✅✅ |
|
| NOT | “Flip it! True becomes false, false becomes true” 🔄 |
Logical operator is also used perform operation between operands. It also gives output in the boolean form(true/false) In it there are 2 operator &&(AND), ||(OR)
In AND(&&) operator if all condition is true only then it gives true output, if even a single one gives false output then the whole output becomes false
eg:-(4>2 && 2>1 && 3>4)=>false
it gives false output because first two condition is true but the last one is false, so it gives the output false.
In OR( || ) operator it just opposite of AND operator in it if even a single one is true then it give the true output.
eg:- (4>2 || 2>1 || 3>4)=>true
In NOT(!)
When you toss the coin and you get head then it becomes tails and in next time when you flip and then you get tail then it becomes head. In it NOT operator just give the opposite of the answer you get(true becomes false, false becomes true)
Unary operator
It is used only in single operand. It works on just one value — like a solo player doing all the action alone.
You have a glass of water. You can either:
Fill more water =
++
Drink some =
--
Check if it's empty (true/false) =
!
That’s unary operators in action —Ek hi cheez par kaam karta hai.
Ternary operator
It's Java's shortcut for an if-else statement.
Like saying:
“Bro, don’t write 5 lines… just say it in 1 line!”
condition ? value_if_true : value_if_false;
"If this is true? Do this : Otherwise, do that"
Java be like:
“Are you hungry?”
If yes,
If no,
int age = 18;
String result = (age >= 18) ? "You can vote" : "Too young, bro!";
System.out.println(result);//
output:you can vote
new
1.new is a java keyword which is used to create object.
2.We can create object for predefined or user defined class.
3.creating an object is nothing but allocating the memory so that we can use in the application.
4.Once an object is created then object will be allocated in the heap memory.
Imagine you’re in a toy factory
You have a blueprint of a teddy bear (called a class)
But you want to make an actual teddy to hug
eg:-
Teddy myTeddy = new Teddy();
Program
//Declaration of class
class Teddy{
int height=10;
public void teddy{
System.out.println(height);
}
Teddy myTeddy=new Teddy();
}
What does new
keyword do
It allocates memory for the object
Calls the constructor of the class
And gives you a fresh, usable object
. Operator
Dot (.
) = Java ka “access karne wala remote control”
It lets you access stuff inside an object, class, or package.
Where does we use (.) dot operator
Prog:
import java.util.Scanner;
public class Car{
String color=”Red”;
public void start(){
System.out.println(“Car Start”);
}
public static void main(String args[]){
Car car = new Car();
car.color;
car.start();
}
Access method |
| Tell the car to start |
Access variable |
| Ask car what color it is |
Access package/class |
| Find |
Conditionals
1. Conditionals (aka If-Else Wale Faisle)
Java be like: "Check karo… phir decide karo kya karna hai."
🍔 Lame Analogy:
You're at a momo shop.
javaCopyEditif (money >= 50) {
buyMomos();
} else {
cryInCorner();
}
Java be like: “Paise hain? Toh momos ✅ Nahi? Toh rona ❌”
That’s if-else logic — “check condition, take action” 🎮✅ Types:
if
→ check one condition
if-else
→ choose between two
else if
→ multiple options
switch
→ like a menu card 🍽️: pick something from many choices2. Loops (aka Kaam baar-baar karne ki machine)
Java be like: "Jab tak bolta hai, main repeat karta rahunga!"
Lame Analogy:
You’re eating fries one by one from a big packet.
javaCopyEditwhile (friesLeft) { eatFries(); }
Java be like:
“Fries khatam nahi hue? Toh khate raho! ”
🔁 Types of Loops:
Loop Type Lame Meaning while
“Jab tak condition true hai, chalte raho” 🏃♂️ do-while
“Pehle ek baar karo, fir check karo” 😏 for
“Mujhe batao kitni baar repeat karna hai” 🧾 for (int i = 1; i <= 5; i++) { System.out.println("Hi " + i); }
Conditionals = Java ka dimaag ("soch ke faisla lo")
Loops = Java ka stamina ("jab tak bolta hai, karta rahu")
Subscribe to my newsletter
Read articles from Khelendra Kumar Thakur directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
