या chapter मध्ये आपण Java मधील Arithmetic Operators, Assignment Operators, Comparison Operators, Logical Operators, Increment and Decrement, Unary Operators आणि Ternary Operator हे concepts एकमेकांशी logically connect करून शिकणार आहोत.
हा chapter Java SE 26 language semantics शी aligned आहे. Java SE 26 March 2026 मध्ये released झाले असून सध्या published Java Language Specification ची current stable edition आहे. या chapter मधील operators हे Java language fundamentals आहेत; त्यांच्या core semantics या version मध्येही specification-defined आहेत.
Learning Outcomes#
हा chapter पूर्ण केल्यानंतर तुम्ही independently:
- Java मध्ये operator आणि operand म्हणजे काय हे explain करू शकाल.
- arithmetic calculations साठी योग्य operator निवडू शकाल.
- integer division आणि floating-point division मधला फरक reason करू शकाल.
%remainder operator practical problems मध्ये वापरू शकाल.- simple आणि compound assignment operators वापरू शकाल.
- comparison expressions चे
booleanresults predict करू शकाल. - multiple conditions
&&,||आणि!ने combine करू शकाल. - short-circuit evaluation का useful आहे हे explain करू शकाल.
- prefix आणि postfix
++/--मधला फरक predict करू शकाल. - unary
+,-,!operators योग्य context मध्ये वापरू शकाल. - simple decisions साठी ternary operator वापरू शकाल.
- operator precedence मुळे expression चा result कसा बदलतो हे समजू शकाल.
- common operator mistakes identify आणि correct करू शकाल.
- beginner-level Java operator interview questions confidently answer करू शकाल.
Operator म्हणजे काय?#
An operator is a symbol that performs an operation on one or more operands and produces a result.
Operator म्हणजे एखाद्या value किंवा values वर operation करणारा special symbol.
उदाहरण:
int total = 100 + 50;इथे:
100→ operand50→ operand+→ operator150→ operation चा result=→ assignment operator
म्हणजे एका statement मध्ये एकापेक्षा जास्त operators असू शकतात.
Operand म्हणजे काय?#
An operand is a value, variable, or expression on which an operator operates.
उदाहरण:
int quantity = 4;
int price = 250;
int total = quantity * price;quantity आणि price हे * operator चे operands आहेत.
Result:
1000Real project मध्ये operators almost प्रत्येक calculation आणि condition मध्ये दिसतात.
उदाहरण:
double finalPrice = price - discount;
boolean eligible = age >= 18;
stock -= soldQuantity;
orderCount++;आता operators एकेक करून समजून घेऊ.
Arithmetic Operators#
Arithmetic operators perform mathematical calculations on numeric operands.
Arithmetic operators numeric values वर mathematical calculations करण्यासाठी वापरले जातात.
Java मधील beginner-level arithmetic operators:
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | 10 + 5 |
- | Subtraction | 10 - 5 |
* | Multiplication | 10 * 5 |
/ | Division | 10 / 5 |
% | Remainder | 10 % 3 |
Addition +#
int productPrice = 1200;
int deliveryCharge = 100;
int amount = productPrice + deliveryCharge;
System.out.println(amount);Expected Output:
1300Practical meaning#
E-commerce application मध्ये:
Final Amount = Product Price + Delivery Chargeत्यामुळे + naturally वापरला जातो.
Subtraction -#
int walletBalance = 5000;
int purchaseAmount = 1750;
int remainingBalance = walletBalance - purchaseAmount;
System.out.println(remainingBalance);Expected Output:
3250Multiplication *#
Requirement:
एका product ची किंमत ₹750 आहे आणि customer ने 4 units घेतले.
int unitPrice = 750;
int quantity = 4;
int subtotal = unitPrice * quantity;
System.out.println(subtotal);Expected Output:
3000Division /#
Division beginner साठी deceptively simple आहे.
int result = 10 / 2;
System.out.println(result);Output:
5पण हे पहा:
int result = 5 / 2;
System.out.println(result);Output:
22.5 का नाही?
कारण दोन्ही operands int आहेत.
Integer Division#
Integer division between integer operands produces an integer result by discarding the fractional part.
Java integer division मध्ये fractional part remove होतो; result zero कडे truncate होतो. Java Language Specification integer division साठी truncation toward zero define करते.
int result = 7 / 2;
System.out.println(result);Output:
3Important#
हे rounding नाही.
7 / 2 = 3.5पण integer division:
3Floating-Point Division#
Decimal result हवा असेल तर किमान एक operand floating-point असणे आवश्यक आहे.
double result = 5.0 / 2;
System.out.println(result);Output:
2.5हे सुद्धा चालेल:
double result = 5 / 2.0;
System.out.println(result);Output:
2.5एक common trap#
double result = 5 / 2;
System.out.println(result);Output:
2.0Beginner ला वाटू शकते:
Variabledoubleआहे, मग result2.5असायला हवा.
पण evaluation अशी होते:
5 / 2
↓
integer division
↓
2
↓
double मध्ये assignment
↓
2.0Variable चा destination type division आधी बदल घडवत नाही.
Remainder Operator %#
The remainder operator returns the remainder left after division.
% आपल्याला division नंतर उरलेली value देतो.
int remainder = 10 % 3;
System.out.println(remainder);Calculation:
10 ÷ 3
3 × 3 = 9
10 - 9 = 1Output:
1% चा practical use — Even किंवा Odd#
int number = 14;
boolean isEven = number % 2 == 0;
System.out.println(isEven);Output:
trueकारण even number ला 2 ने divide केल्यावर remainder 0 येतो.
14 % 2 → 0Odd number:
15 % 2 → 1% चा practical use — Complete Groups#
Suppose 23 students आहेत आणि एका team मध्ये 5 students ठेवायचे आहेत.
int students = 23;
int teamSize = 5;
int completeTeams = students / teamSize;
int remainingStudents = students % teamSize;
System.out.println(completeTeams);
System.out.println(remainingStudents);Output:
4
3Meaning:
- 4 complete teams
- 3 students remaining
Negative remainder#
Java मध्ये remainder च्या sign वर dividend चा प्रभाव असतो.
System.out.println(-10 % 4);Output:
-2Beginner stage ला हा rule recognize करणे पुरेसे आहे.
Division by Zero#
Integer division मध्ये zero divisor वापरल्यास runtime error येतो.
int result = 10 / 0;Program run झाल्यावर ArithmeticException येते.
Important distinction#
double result = 10.0 / 0.0;
System.out.println(result);Floating-point arithmetic चे rules वेगळे आहेत आणि result infinity representation होऊ शकतो.
या chapter साठी core rule:
Integer division by zero is invalid at runtime.
Assignment Operators#
An assignment operator stores or updates a value in a variable.
Assignment operator variable मध्ये value assign किंवा existing value update करण्यासाठी वापरला जातो.
सर्वात basic assignment operator:
=उदाहरण:
int salary = 50000;50000 ही value salary variable मध्ये assign झाली.
Assignment flow#
Right Side Evaluate
↓
Result
↓
Assignment Operator =
↓
Left VariableExample:
int basePrice = 1000;
int tax = 180;
int finalPrice = basePrice + tax;पहिले:
basePrice + taxevaluate होईल.
1000 + 180 → 1180मग:
finalPrice = 1180Compound Assignment Operators#
Repeated update operations साठी Java compound assignment देते.
| Operator | Example | Basic Meaning |
|---|---|---|
+= | x += 5 | add and assign |
-= | x -= 5 | subtract and assign |
*= | x *= 5 | multiply and assign |
/= | x /= 5 | divide and assign |
%= | x %= 5 | remainder and assign |
+=#
int wallet = 1000;
wallet += 500;
System.out.println(wallet);Output:
1500Conceptually:
wallet = wallet + 500;-=#
Inventory example:
int stock = 50;
int soldQuantity = 8;
stock -= soldQuantity;
System.out.println(stock);Output:
42*=#
int points = 100;
points *= 2;
System.out.println(points);Output:
200/=#
int amount = 1000;
amount /= 4;
System.out.println(amount);Output:
250%=#
int value = 17;
value %= 5;
System.out.println(value);Output:
2Compound Assignment ची एक important Java behavior#
हे दोन्ही नेहमी type-system च्या दृष्टीने identical नसतात:
short number = 10;
number += 5;हे valid आहे.
पण:
short number = 10;
number = number + 5;हे compile होत नाही, कारण arithmetic expression मध्ये numeric promotion मुळे result int होतो.
Compound assignment मध्ये Java आवश्यक conversion internally apply करू शकते.
Java Language Specification compound assignment ला broadly:
E1 op= E2हे E1 चा type preserve करण्यासाठी conversion असलेल्या assignment प्रमाणे define करते, तसेच left side एकदाच evaluate होते.
Beginner साठी takeaway:
x += y हे फक्त typing shortcut म्हणून पाहू नका; Java type rules मध्ये त्याचे काही additional semantics आहेत.Deep casting rules पुढील योग्य chapter मध्ये शिकता येतील.
Comparison Operators#
Comparison operators compare operands and produce a boolean result.
Comparison operators दोन values compare करतात आणि result:
trueकिंवा:
falseअसा मिळतो.
Comparison Operators Table#
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
Equal To ==#
int enteredPin = 1234;
int correctPin = 1234;
boolean matched = enteredPin == correctPin;
System.out.println(matched);Output:
trueVery Important#
= आणि == वेगळे आहेत.
= Assignment
== Comparisonहा beginner programming मधला अत्यंत common confusion आहे.
Not Equal !=#
int currentStock = 10;
boolean outOfStock = currentStock != 0;
System.out.println(outOfStock);Output:
trueVariable च्या नावाकडे लक्ष द्या: या example मध्ये expression technically "stock is not zero" तपासत आहे, त्यामुळे better naming असेल:
boolean stockAvailable = currentStock != 0;Readable code मध्ये variable name आणि condition चा meaning match झाला पाहिजे.
Greater Than >#
int score = 82;
boolean abovePassingThreshold = score > 40;
System.out.println(abovePassingThreshold);Output:
trueLess Than <#
int availableStock = 3;
int reorderLevel = 5;
boolean needsReorder = availableStock < reorderLevel;
System.out.println(needsReorder);Output:
trueGreater Than or Equal To >=#
Requirement:
User चे age 18 किंवा त्यापेक्षा जास्त असेल तर eligibility true.
int age = 18;
boolean eligible = age >= 18;
System.out.println(eligible);Output:
trueजर आपण > वापरला असता:
age > 18तर exactly 18-year-old user incorrectly exclude झाला असता.
हा boundary-condition bug आहे.
Less Than or Equal To <=#
int attemptsUsed = 2;
int maximumAttempts = 3;
boolean canContinue = attemptsUsed <= maximumAttempts;
System.out.println(canContinue);Output:
trueRequirement च्या wording वरून operator निवडणे अत्यंत important आहे.
Comparison Chain लिहिता येते का?#
Mathematics मध्ये:
1 < x < 10सहज दिसते.
पण Java मध्ये:
boolean result = 1 < x < 10;हे valid नाही.
का?
पहिली expression:
1 < xयाचा result boolean येतो.
नंतर effectively:
boolean < 10असा invalid comparison होईल.
Correct Java expression:
boolean result = x > 1 && x < 10;Logical operator आता useful होतो.
== बद्दल एक boundary note#
Primitive numeric आणि boolean values च्या examples मध्ये == value comparison साठी वापरला जातो.
Reference types, विशेषतः String, यांच्या बाबतीत == चे semantics वेगळे असतात.
या chapter मध्ये ते deep शिकणार नाही, कारण object/reference equality हा स्वतंत्र concept आहे.
आत्तासाठी:
Text content compare करताना blindly == वापरण्याची habit बनवू नका.Logical Operators#
Logical operators combine or negate boolean expressions to produce a boolean result.
Real applications मध्ये एकच condition अनेकदा पुरेशी नसते.
Requirement:
User चे age कमीतकमी 18 असावे आणि account active असावे.
आपल्याकडे दोन conditions आहेत:
age >= 18आणि:
accountActiveदोन्ही combine करण्यासाठी logical operator लागतो.
Core Logical Operators#
| Operator | Meaning | ||
|---|---|---|---|
&& | Logical AND | ||
| ` | ` | Logical OR | |
! | Logical NOT |
Java मध्ये boolean operands साठी &, |, ^ सुद्धा defined आहेत, पण && आणि || short-circuit करतात. Integer operands वर &, |, ^ ची bitwise meaning असते; ती या chapter च्या scope बाहेर आहे. Java specification boolean logical operators आणि conditional && / || यांना वेगवेगळे define करते.
Logical AND &&#
The && operator produces true only when both boolean operands are true.
Truth table:
| Left | Right | Result |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Example:
int age = 25;
boolean accountActive = true;
boolean eligible = age >= 18 && accountActive;
System.out.println(eligible);Output:
trueदोन्ही conditions true असल्यामुळे final result true.
Logical OR ||#
The || operator produces true when at least one operand is true.
Truth table:
| Left | Right | Result |
|---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Requirement:
Customer premium member किंवा order amount ₹5000 पेक्षा जास्त असेल तर free delivery.
boolean premiumMember = false;
int orderAmount = 6500;
boolean freeDelivery =
premiumMember || orderAmount >= 5000;
System.out.println(freeDelivery);Output:
trueपहिली condition false असूनही दुसरी true आहे.
Logical NOT !#
The ! operator reverses a boolean value.
!true → false
!false → trueExample:
boolean accountBlocked = false;
boolean canLogin = !accountBlocked;
System.out.println(canLogin);Output:
trueShort-Circuit Evaluation#
हा practical Java programming मधला खूप important concept आहे.
&& Short-Circuit#
Java && मध्ये left operand false आला तर right operand evaluate केला जात नाही.
Example:
int divisor = 0;
boolean valid =
divisor != 0 && 100 / divisor > 5;
System.out.println(valid);Output:
falseमहत्त्वाचे काय झाले?
पहिली condition:
divisor != 0Result:
false&& ला final result आधीच समजला:
false && anything → falseत्यामुळे:
100 / divisorexecuteच झाले नाही.
म्हणून integer divide-by-zero error टळला.
|| Short-Circuit#
|| मध्ये left operand true असेल तर right operand evaluate करण्याची आवश्यकता नसते.
boolean premiumMember = true;
int amount = 1000;
boolean specialAccess =
premiumMember || amount > 5000;premiumMember already true असल्यामुळे final OR result true आहे.
Short-Circuit चा mental model#
&&#
पहिली condition false?
↓
होय
↓
Final result false
↓
Right side skip||#
पहिली condition true?
↓
होय
↓
Final result true
↓
Right side skipIncrement and Decrement Operators#
Increment ++#
The increment operator increases a numeric variable by one.
int orderCount = 10;
orderCount++;
System.out.println(orderCount);Output:
11Equivalent basic idea:
orderCount = orderCount + 1;किंवा:
orderCount += 1;Decrement --#
The decrement operator decreases a numeric variable by one.
int remainingAttempts = 3;
remainingAttempts--;
System.out.println(remainingAttempts);Output:
2Prefix आणि Postfix#
++ आणि -- variable च्या आधी किंवा नंतर येऊ शकतात.
++x Prefix increment
x++ Postfix increment
--x Prefix decrement
x-- Postfix decrementVariable standalone statement मध्ये वापरल्यास:
x++;आणि:
++x;दोन्ही शेवटी x एकाने वाढवतात.
Difference तेव्हा स्पष्ट दिसतो जेव्हा expression ला result value म्हणून वापरले जाते.
Postfix Increment x++#
Postfix increment yields the current value first, then increments the variable.
int x = 5;
int y = x++;
System.out.println(x);
System.out.println(y);Output:
6
5Execution:
x = 5
y = x++
↓
y ला current x → 5
नंतर x increment
↓
x = 6Prefix Increment ++x#
Prefix increment increments the variable first, then yields the updated value.
int x = 5;
int y = ++x;
System.out.println(x);
System.out.println(y);Output:
6
6Execution:
x = 5
++x
↓
x becomes 6
↓
expression value = 6
↓
y = 6Postfix Decrement#
int x = 5;
int y = x--;
System.out.println(x);
System.out.println(y);Output:
4
5Prefix Decrement#
int x = 5;
int y = --x;
System.out.println(x);
System.out.println(y);Output:
4
4Avoid Clever Increment Expressions#
Java technically अशा expressions evaluate करू शकते:
int x = 5;
int result = x++ + ++x;पण production code मध्ये अशी expression unnecessarily confusing आहे.
Step:
x = 5
x++ gives 5
x becomes 6
++x makes x 7
++x gives 7
result = 5 + 7
= 12Final:
x = 7
result = 12हे समजणे useful आहे.
असे code लिहिणे recommended नाही.
Better approach#
int x = 5;
int firstValue = x;
x++;
x++;
int secondValue = x;
int result = firstValue + secondValue;Readable code debugging आणि maintenance सोपे करते.
Unary Operators#
A unary operator operates on a single operand.
Binary operator ला दोन operands लागतात:
10 + 5Unary operator ला एक operand लागतो:
-10या chapter मध्ये relevant unary operators:
| Operator | Purpose |
|---|---|
+ | Unary plus |
- | Unary minus |
! | Logical complement |
++ | Increment |
-- | Decrement |
~ | Bitwise complement |
~ हा unary operator आहे, पण त्याचा meaningful explanation binary/bitwise representation सोबत करणे योग्य आहे. Bitwise operations या supplied topics मध्ये नसल्यामुळे इथे त्याची फक्त recognition-level माहिती ठेवू.
Unary Plus +#
int number = 10;
int result = +number;
System.out.println(result);Output:
10Unary plus सामान्यतः value चा sign बदलत नाही.
Unary Minus -#
int balanceChange = 500;
int reversal = -balanceChange;
System.out.println(reversal);Output:
-500Important:
int number = -10;इथे - negative literal चा भाग म्हणून दिसत असला तरी language expression level वर unary minus operator म्हणून समजणे useful आहे.
Logical Complement !#
हा देखील unary operator आहे कारण त्याला फक्त एक boolean operand लागतो.
boolean paymentFailed = false;
boolean paymentSuccessful = !paymentFailed;
System.out.println(paymentSuccessful);Output:
trueDouble Negation#
boolean active = true;
System.out.println(!!active);Output:
trueReason:
active → true
!active → false
!!active → trueProduction code मध्ये unnecessary double negation avoid केल्यास readability चांगली राहते.
Ternary Operator#
The conditional operator ?: evaluates a boolean condition and yields one of two expressions based on the result.
याला सामान्यतः ternary operator म्हणतात कारण याला तीन operands असतात.
Syntax:
condition ? valueIfTrue : valueIfFalseJava specification मध्ये याचे official नाव conditional operator ? : आहे. First expression boolean किंवा Boolean असावी आणि condition वरून second किंवा third expression पैकी appropriate expression evaluate होते.
Basic Example#
Requirement:
Marks 40 किंवा जास्त असतील तर"Pass", अन्यथा"Fail".
int marks = 65;
String result = marks >= 40 ? "Pass" : "Fail";
System.out.println(result);Output:
PassTernary execution flow#
marks >= 40
↓
true?
/ \
yes no
/ \
"Pass" "Fail"
\ /
resultTernary returns a value#
हे खूप important आहे.
String status =
marks >= 40 ? "Pass" : "Fail";पूर्ण ternary expression एक value produce करते.
त्यामुळे आपण ती:
- variable मध्ये assign करू शकतो
- expression मध्ये वापरू शकतो
- method argument म्हणून वापरू शकतो
Numeric Ternary Example#
int stock = 7;
int reorderQuantity =
stock < 10 ? 20 : 0;
System.out.println(reorderQuantity);Output:
20Boolean Ternary — अनेकदा unnecessary#
Weak:
boolean eligible =
age >= 18 ? true : false;Better:
boolean eligible = age >= 18;का?
age >= 18 itself already boolean result देते.
Ternary लावून आपण त्याच boolean ला unnecessarily पुन्हा convert करत आहोत.
Nested Ternary#
Technically valid:
int marks = 72;
String grade =
marks >= 75 ? "A"
: marks >= 60 ? "B"
: "C";Conditional operator right-associative आहे.
पण beginner production code मध्ये deeply nested ternary readability कमी करू शकते.
Simple decision → ternary योग्य.
Complex multi-branch business logic → clearer control-flow structure generally better.
त्याचे detail पुढील relevant chapter मध्ये शिकू.
Operator Precedence#
एका expression मध्ये multiple operators असल्यास Java कोणता आधी apply करतो?
Example:
int result = 10 + 5 * 2;आपण left-to-right blindly केल्यास:
10 + 5 = 15
15 * 2 = 30पण actual result:
20कारण * ची precedence + पेक्षा higher आहे.
Execution:
5 * 2 → 10
10 + 10 → 20Relevant Precedence — High to Low#
| Priority | Operators | ||
|---|---|---|---|
| Higher | postfix x++, x-- | ||
unary ++x, --x, +, -, !, ~ | |||
*, /, % | |||
+, - | |||
<, <=, >, >= | |||
==, != | |||
& | |||
^ | |||
| `\ | ` | ||
&& | |||
| `\ | \ | ` | |
?: | |||
| Lower | =, +=, -=, *=, /=, %= |
Parentheses वापरणे#
तुम्हाला precedence पाठ असली तरी complex business expression मध्ये parentheses readability improve करतात.
Without explicit grouping:
boolean eligible =
age >= 18 && score >= 60 || premiumMember;Java precedence मुळे हे logically:
boolean eligible =
(age >= 18 && score >= 60) || premiumMember;असे evaluate होते.
पण business requirement कोणती आहे हे readable code मध्ये स्पष्ट असले पाहिजे.
Better:
boolean eligible =
(age >= 18 && score >= 60)
|| premiumMember;Precedence आणि Evaluation Order एकच गोष्ट नाही#
हा subtle पण useful distinction आहे.
Precedence सांगते expression कसा grouped आहे.
Evaluation order सांगतो operands runtime ला कोणत्या क्रमाने evaluate होतात.
Java expressions मध्ये operands generally left-to-right evaluate होतात; &&, ||, आणि conditional operator मध्ये काही operands conditionally skip होऊ शकतात. Operator grammar precedence rules आणि short-circuit behavior Java Language Specification मध्ये explicitly defined आहेत.
Beginner takeaway:
Precedence result समजण्यासाठी वापरा; side effects असलेल्या complicated expressions लिहिण्याची सवय बनवू नका.
2. Practical / Real-World Application#
Scenario — E-Commerce Order Calculation#
Requirement:
- Product unit price = ₹1200
- Quantity = 3
- Premium customer आहे.
- ₹3000 किंवा जास्त subtotal आणि premium membership असल्यास 10% discount.
- Discount नंतर amount ₹3000 किंवा जास्त असल्यास shipping free.
- अन्यथा ₹100 shipping.
- Purchase नंतर stock कमी करा.
- Successful order counter एकाने वाढवा.
public class OrderCalculation {
public static void main(String[] args) {
int unitPrice = 1200;
int quantity = 3;
int stock = 10;
int orderCount = 0;
boolean premiumCustomer = true;
int subtotal = unitPrice * quantity;
boolean discountEligible =
premiumCustomer && subtotal >= 3000;
double discount =
discountEligible ? subtotal * 0.10 : 0;
double amountAfterDiscount =
subtotal - discount;
double shippingCharge =
amountAfterDiscount >= 3000 ? 0 : 100;
double finalAmount =
amountAfterDiscount + shippingCharge;
stock -= quantity;
orderCount++;
System.out.println("Subtotal: " + subtotal);
System.out.println("Discount Eligible: " + discountEligible);
System.out.println("Discount: " + discount);
System.out.println("Shipping: " + shippingCharge);
System.out.println("Final Amount: " + finalAmount);
System.out.println("Remaining Stock: " + stock);
System.out.println("Order Count: " + orderCount);
}
}Expected Output:
Subtotal: 3600
Discount Eligible: true
Discount: 360.0
Shipping: 0.0
Final Amount: 3240.0
Remaining Stock: 7
Order Count: 1Operators used#
Arithmetic#
unitPrice * quantity
subtotal * 0.10
subtotal - discount
amountAfterDiscount + shippingChargeComparison#
subtotal >= 3000
amountAfterDiscount >= 3000Logical#
premiumCustomer && subtotal >= 3000Ternary#
discountEligible ? subtotal * 0.10 : 0Compound Assignment#
stock -= quantity;Increment#
orderCount++;हा एक realistic example दाखवतो की operators independent syntax नसून actual business logic तयार करण्यासाठी combine केले जातात.
Requirement → Operator Thinking#
Professional developer म्हणून requirement वाचताना keywords ओळखण्याची habit useful आहे.
| Requirement wording | Likely operator thinking | ||
|---|---|---|---|
| total of | + | ||
| difference | - | ||
| price × quantity | * | ||
| distribute equally | / | ||
| remaining | % | ||
| at least | >= | ||
| at most | <= | ||
| greater than | > | ||
| less than | < | ||
| equal | == | ||
| not equal | != | ||
| both conditions | && | ||
| either condition | `\ | \ | ` |
| opposite condition | ! | ||
| increase by one | ++ | ||
| decrease by one | -- | ||
| update existing amount | compound assignment | ||
| simple either/or value | ?: |
3. Common Mistakes & Misconceptions#
Mistake 1 — = आणि == same समजणे#
Wrong understanding:
= means equalCorrect understanding:
= assigns
== comparesExample:
int age = 18;
boolean adult = age == 18;Mistake 2 — 5 / 2 म्हणजे 2.5#
System.out.println(5 / 2);Actual:
2Root Cause#
दोन्ही operands integers आहेत.
Fix#
System.out.println(5.0 / 2);Output:
2.5Mistake 3 — Result double मध्ये ठेवला म्हणजे decimal division होईल#
Wrong:
double result = 5 / 2;Actual:
2.0Better:
double result = 5.0 / 2;Mistake 4 — % म्हणजे percentage#
Programming मध्ये:
%हा operator percentage operator नाही.
तो remainder operator आहे.
10 % 3Result:
110% discount calculate करण्यासाठी:
double discount = amount * 0.10;Mistake 5 — >= requirement ला > लिहिणे#
Requirement:
Minimum age 18.
Wrong:
age > 18Correct:
age >= 1818 boundary case miss करू नका.
Mistake 6 — Mathematical chained comparison#
Wrong:
1 < age < 60Correct:
age > 1 && age < 60Mistake 7 — && आणि || interchange करणे#
Requirement:
User must be active AND verified.
Wrong:
active || verifiedहे एका condition true असली तरी access देईल.
Correct:
active && verifiedMistake 8 — ! चा meaning चुकीचा घेणे#
!loggedInयाचा अर्थ:
not logged inतो variable change करत नाही.
तो boolean expression चे opposite result produce करतो.
Mistake 9 — Prefix/Postfix difference ignore करणे#
int x = 5;
int y = x++;y = 5.
पण:
int x = 5;
int y = ++x;y = 6.
Mistake 10 — Clever increment expressions#
Avoid:
int result = x++ + ++x;जरी output predict करता येत असला तरी readability खराब होते.
Mistake 11 — Ternary प्रत्येक decision साठी वापरणे#
Readable:
String result =
marks >= 40 ? "Pass" : "Fail";Hard to maintain:
String grade =
marks >= 90 ? "A+" :
marks >= 80 ? "A" :
marks >= 70 ? "B" :
marks >= 60 ? "C" :
marks >= 40 ? "D" : "F";Nested ternary technically possible असला तरी readability ही production consideration आहे.
Mistake 12 — Precedence blindly rely करणे#
boolean allowed =
active && verified || admin;Compiler ला meaning clear आहे.
Human reader ला business grouping लगेच clear नसेल.
Better:
boolean allowed =
(active && verified) || admin;4. Hands-On Practice#
Exercise 1 — Order Total#
Problem#
Product price ₹850 आहे आणि quantity 4 आहे.
Learner Task#
Total price calculate करा.
Hint 1#
Quantity आणि price combine करायचे आहेत.
Hint 2#
Multiplication operator वापरा.
Solution#
int price = 850;
int quantity = 4;
int total = price * quantity;
System.out.println(total);Expected Output:
3400Exercise 2 — Complete Boxes#
Problem#
53 items आहेत. एका box मध्ये 10 items बसतात.
Find:
- complete boxes
- remaining items
Hints#
Complete boxes → /
Remaining items → %
Solution#
int items = 53;
int boxCapacity = 10;
int completeBoxes = items / boxCapacity;
int remainingItems = items % boxCapacity;
System.out.println(completeBoxes);
System.out.println(remainingItems);Output:
5
3Exercise 3 — Discount Eligibility#
Requirement#
Customer:
- premium असला पाहिजे
- आणि order ₹2500 किंवा जास्त असली पाहिजे
Solution#
boolean premium = true;
int amount = 3000;
boolean eligible =
premium && amount >= 2500;
System.out.println(eligible);Output:
trueExercise 4 — Free Delivery#
Requirement#
Free delivery मिळेल जर:
- customer premium असेल
- OR
- order amount ₹5000 किंवा जास्त असेल
Solution#
boolean premium = false;
int amount = 5200;
boolean freeDelivery =
premium || amount >= 5000;
System.out.println(freeDelivery);Output:
trueExercise 5 — Stock Update#
Problem#
Starting stock = 25.
7 items sold.
Constraint#
Compound assignment वापरा.
Solution#
int stock = 25;
stock -= 7;
System.out.println(stock);Output:
18Exercise 6 — Predict Prefix/Postfix#
Attempt करण्यापूर्वी answer पाहू नका.
int value = 10;
int first = value++;
int second = ++value;
System.out.println(value);
System.out.println(first);
System.out.println(second);Solution Reasoning#
Initially:
value = 10value++:
first = 10
value = 11++value:
value = 12
second = 12Output:
12
10
12Exercise 7 — Ternary Status#
Requirement#
Balance 0 पेक्षा जास्त असल्यास "Positive" अन्यथा "Zero or Negative".
Solution#
int balance = -20;
String status =
balance > 0
? "Positive"
: "Zero or Negative";
System.out.println(status);Output:
Zero or NegativeExercise 8 — Range Check#
Requirement#
Score 0 ते 100 inclusive आहे का ते check करा.
Think#
Inclusive म्हणजे:
score >= 0आणि:
score <= 100Solution#
int score = 84;
boolean valid =
score >= 0 && score <= 100;
System.out.println(valid);Output:
true5. Interview Preparation#
Q1. What is an operator in Java?#
Answer: An operator is a symbol that performs an operation on one or more operands and produces a result. Examples include arithmetic operators such as +, comparison operators such as >=, and logical operators such as &&.
What the interviewer is testing: Basic understanding of expressions.
Common weak answer: “An operator is used only for calculations.” Operators are also used for assignment, comparison, logical operations, and other expression operations.
Q2. What is the difference between = and ==?#
Answer: = is the assignment operator. It stores a value in a variable. == is an equality operator that compares compatible operands and returns a boolean result.
int x = 10;
boolean result = x == 10;Q3. What is the output of 5 / 2 in Java?#
Answer: The result is 2 because both operands are integers, so Java performs integer division and discards the fractional part.
Q4. How can you get 2.5 from dividing 5 by 2?#
Answer:
At least one operand should be floating-point.
double result = 5.0 / 2;The result is 2.5.
Q5. What does the % operator do?#
Answer: The % operator returns the remainder after division.
10 % 3produces:
1It is commonly used for tasks such as checking whether a number is even or odd.
Q6. What are compound assignment operators?#
Answer: Compound assignment operators combine an operation with assignment.
For example:
x += 5;updates x by adding 5 and storing the result back in x.
Common examples are +=, -=, *=, /=, and %=.
Q7. What do comparison operators return?#
Answer: Comparison operators produce a boolean result: true or false.
For example:
10 > 5evaluates to:
trueQ8. What is the difference between > and >=?#
Answer: > means strictly greater than, while >= means greater than or equal to.
If the minimum valid age is 18:
age >= 18includes age 18, but:
age > 18does not.
Q9. What is the difference between && and ||?#
Answer: && returns true only when both conditions are true. || returns true when at least one condition is true.
active && verifiedrequires both conditions.
premium || orderAmount >= 5000requires at least one condition.
Q10. What is short-circuit evaluation?#
Answer: Short-circuit evaluation means Java may skip evaluating the right operand of && or || when the final result is already known.
For &&, the right operand is skipped when the left operand is false.
For ||, the right operand is skipped when the left operand is true.
Q11. Why is short-circuit evaluation useful?#
Answer: It can avoid unnecessary computation and can safely guard expressions that would otherwise be invalid at runtime.
Example:
divisor != 0 && 100 / divisor > 5If divisor is zero, the division expression is not evaluated.
Q12. What does the ! operator do?#
Answer: ! is the logical complement operator. It reverses a boolean value.
!trueproduces false, and:
!falseproduces true.
Q13. What is the difference between prefix and postfix increment?#
Answer: Both increase the variable by one, but their expression values differ.
Prefix:
++xincrements first and then yields the updated value.
Postfix:
x++yields the current value first and then increments the variable.
Q14. What is the output?#
int x = 5;
int y = x++;
System.out.println(x);
System.out.println(y);Answer:
6
5x++ yields 5 first and then increments x to 6.
Q15. What is the output?#
int x = 5;
int y = ++x;
System.out.println(x);
System.out.println(y);Answer:
6
6++x increments x before its value is used.
Q16. What is a unary operator?#
Answer: A unary operator operates on one operand.
Examples include:
-number
!condition
++count
--countQ17. What is the ternary operator?#
Answer: The ternary operator, formally the conditional operator ?:, selects one of two expressions based on a boolean condition.
Syntax:
condition ? valueIfTrue : valueIfFalseExample:
String result = marks >= 40 ? "Pass" : "Fail";Q18. When should you avoid the ternary operator?#
Answer: Avoid it when the decision logic becomes deeply nested or difficult to read. It is best suited for concise value-selection expressions.
Q19. What is operator precedence?#
Answer: Operator precedence determines how operators in an expression are grouped.
For example:
10 + 5 * 2evaluates multiplication first, producing 20.
Q20. How can you make a complex expression easier to understand?#
Answer: Use meaningful variable names and parentheses to make the intended grouping explicit instead of relying only on memorized precedence rules.
Q21. Is 1 < x < 10 valid Java?#
Answer: No.
The correct expression is:
x > 1 && x < 10The first comparison already produces a boolean, so Java cannot compare that boolean directly with 10 using <.
Q22. What happens when integer division uses zero as the divisor?#
Answer: Integer division by zero causes an ArithmeticException at runtime.
Q23. Is double result = 5 / 2; equal to 2.5?#
Answer: No. 5 / 2 is evaluated using integer division first, producing 2. That integer is then converted to double, producing 2.0.
Q24. Are x += y and x = x + y always identical in Java?#
Answer: Not exactly. Compound assignment has defined conversion behavior and evaluates the left-hand operand only once. In some numeric type situations, x += y can compile when x = x + y requires an explicit cast.
6. Quick Revision#
Arithmetic#
+ Addition
- Subtraction
* Multiplication
/ Division
% RemainderInteger operands:
5 / 2→ 2
Floating-point operand:
5.0 / 2→ 2.5
Assignment#
= Assign
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Remainder and assignComparison#
== Equal
!= Not equal
> Greater
< Less
>= Greater or equal
<= Less or equalResult always boolean for the comparisons taught here.
Logical#
&& Both must be true
|| At least one true
! Reverse booleanShort-circuit:
false && ... → right side skipped
true || ... → right side skippedIncrement / Decrement#
x++ use current value, then increment
++x increment, then use new value
x-- use current value, then decrement
--x decrement, then use new valueTernary#
condition ? trueValue : falseValueSimple value selection साठी useful.
Precedence#
Remember the practical sequence:
Unary
↓
* / %
↓
+ -
↓
Comparison
↓
Logical
↓
Ternary
↓
AssignmentComplex expression मध्ये parentheses prefer करा.
7. You Should Now Be Able To#
हा chapter पूर्ण केल्यानंतर तुम्ही:
- Java operator आणि operand explain करू शकता.
- numeric calculations implement करू शकता.
- integer आणि decimal division distinguish करू शकता.
%practical calculations मध्ये वापरू शकता.- existing variable compound assignment ने update करू शकता.
=,==,!=correctly distinguish करू शकता.- requirement मधील boundaries वरून
<,>,<=,>=निवडू शकता. &&,||,!ने business conditions तयार करू शकता.- short-circuit evaluation explain करू शकता.
- prefix/postfix increment output predict करू शकता.
- simple ternary expression लिहू शकता.
- unnecessary ternary आणि confusing increment expressions identify करू शकता.
- precedence-related outputs reason करू शकता.
- operator-based beginner interview questions answer करू शकता.
8. Final Challenge#
Scenario#
Online learning platform वर course purchase calculation करायची आहे.
Given:
Course Price = ₹2000
Number of Courses = 2
Premium User = true
Wallet Balance = ₹5000
Purchase Count = 4Rules:
- Subtotal = course price × quantity.
- Premium user आणि subtotal किमान ₹4000 असेल तर ₹500 discount.
- Payable amount = subtotal - discount.
- Wallet balance payable amount पेक्षा greater than or equal असेल तर purchase possible.
- Purchase possible असेल तर status
"Eligible", अन्यथा"Insufficient Balance". - Purchase count एकाने increase झाल्याचे independent update दाखवा.
- Wallet deduction हा exercise च्या fixed data वर करा.
Attempt First#
Required operators identify करा:
*
&&
>=
-
?:
++
-=Solution#
public class CoursePurchaseChallenge {
public static void main(String[] args) {
int coursePrice = 2000;
int quantity = 2;
boolean premiumUser = true;
int walletBalance = 5000;
int purchaseCount = 4;
int subtotal = coursePrice * quantity;
boolean discountEligible =
premiumUser && subtotal >= 4000;
int discount =
discountEligible ? 500 : 0;
int payableAmount =
subtotal - discount;
boolean canPurchase =
walletBalance >= payableAmount;
String status =
canPurchase
? "Eligible"
: "Insufficient Balance";
walletBalance -= payableAmount;
purchaseCount++;
System.out.println("Subtotal: " + subtotal);
System.out.println("Discount: " + discount);
System.out.println("Payable: " + payableAmount);
System.out.println("Status: " + status);
System.out.println("Wallet Balance: " + walletBalance);
System.out.println("Purchase Count: " + purchaseCount);
}
}Expected Output:
Subtotal: 4000
Discount: 500
Payable: 3500
Status: Eligible
Wallet Balance: 1500
Purchase Count: 5Reasoning#
2000 × 2
↓
4000
premiumUser == true
AND
subtotal >= 4000
↓
true
discount
↓
500
4000 - 500
↓
3500
5000 >= 3500
↓
true
status
↓
Eligible
wallet
5000 - 3500
↓
1500
purchaseCount
4 → 5