CodeLangs AISoftware Training Institute
Java for Beginners (Marathi)/Java Operators
Dashboard
Chapter 9 · Java for Beginners

Java Operators

4,163
words
19
min read
66
practice items
Interactive learning

Practice lab

Question 1 of 28 00:00

या 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 चे boolean results 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.

उदाहरण:

JAVA
int total = 100 + 50;

इथे:

  • 100 → operand
  • 50 → operand
  • + → operator
  • 150 → operation चा result
  • = → assignment operator

म्हणजे एका statement मध्ये एकापेक्षा जास्त operators असू शकतात.


Operand म्हणजे काय?#

An operand is a value, variable, or expression on which an operator operates.

उदाहरण:

JAVA
int quantity = 4;
int price = 250;

int total = quantity * price;

quantity आणि price हे * operator चे operands आहेत.

Result:

TEXT
1000

Real project मध्ये operators almost प्रत्येक calculation आणि condition मध्ये दिसतात.

उदाहरण:

JAVA
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:

OperatorMeaningExample
+Addition10 + 5
-Subtraction10 - 5
*Multiplication10 * 5
/Division10 / 5
%Remainder10 % 3

Addition +#

JAVA
int productPrice = 1200;
int deliveryCharge = 100;

int amount = productPrice + deliveryCharge;

System.out.println(amount);

Expected Output:

TEXT
1300

Practical meaning#

E-commerce application मध्ये:

TEXT
Final Amount = Product Price + Delivery Charge

त्यामुळे + naturally वापरला जातो.


Subtraction -#

JAVA
int walletBalance = 5000;
int purchaseAmount = 1750;

int remainingBalance = walletBalance - purchaseAmount;

System.out.println(remainingBalance);

Expected Output:

TEXT
3250

Multiplication *#

Requirement:

एका product ची किंमत ₹750 आहे आणि customer ने 4 units घेतले.
JAVA
int unitPrice = 750;
int quantity = 4;

int subtotal = unitPrice * quantity;

System.out.println(subtotal);

Expected Output:

TEXT
3000

Division /#

Division beginner साठी deceptively simple आहे.

JAVA
int result = 10 / 2;

System.out.println(result);

Output:

TEXT
5

पण हे पहा:

JAVA
int result = 5 / 2;

System.out.println(result);

Output:

TEXT
2

2.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 करते.

JAVA
int result = 7 / 2;

System.out.println(result);

Output:

TEXT
3

Important#

हे rounding नाही.

TEXT
7 / 2 = 3.5

पण integer division:

TEXT
3

Floating-Point Division#

Decimal result हवा असेल तर किमान एक operand floating-point असणे आवश्यक आहे.

JAVA
double result = 5.0 / 2;

System.out.println(result);

Output:

TEXT
2.5

हे सुद्धा चालेल:

JAVA
double result = 5 / 2.0;

System.out.println(result);

Output:

TEXT
2.5

एक common trap#

JAVA
double result = 5 / 2;

System.out.println(result);

Output:

TEXT
2.0

Beginner ला वाटू शकते:

Variable double आहे, मग result 2.5 असायला हवा.

पण evaluation अशी होते:

TEXT
5 / 2
 ↓
integer division
 ↓
2
 ↓
double मध्ये assignment
 ↓
2.0

Variable चा destination type division आधी बदल घडवत नाही.


Remainder Operator %#

The remainder operator returns the remainder left after division.

% आपल्याला division नंतर उरलेली value देतो.

JAVA
int remainder = 10 % 3;

System.out.println(remainder);

Calculation:

TEXT
10 ÷ 3

3 × 3 = 9

10 - 9 = 1

Output:

TEXT
1

% चा practical use — Even किंवा Odd#

JAVA
int number = 14;

boolean isEven = number % 2 == 0;

System.out.println(isEven);

Output:

TEXT
true

कारण even number ला 2 ने divide केल्यावर remainder 0 येतो.

TEXT
14 % 2 → 0

Odd number:

TEXT
15 % 2 → 1

% चा practical use — Complete Groups#

Suppose 23 students आहेत आणि एका team मध्ये 5 students ठेवायचे आहेत.

JAVA
int students = 23;
int teamSize = 5;

int completeTeams = students / teamSize;
int remainingStudents = students % teamSize;

System.out.println(completeTeams);
System.out.println(remainingStudents);

Output:

TEXT
4
3

Meaning:

  • 4 complete teams
  • 3 students remaining

Negative remainder#

Java मध्ये remainder च्या sign वर dividend चा प्रभाव असतो.

JAVA
System.out.println(-10 % 4);

Output:

TEXT
-2

Beginner stage ला हा rule recognize करणे पुरेसे आहे.


Division by Zero#

Integer division मध्ये zero divisor वापरल्यास runtime error येतो.

JAVA
int result = 10 / 0;

Program run झाल्यावर ArithmeticException येते.

Important distinction#

JAVA
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:

TEXT
=

उदाहरण:

JAVA
int salary = 50000;

50000 ही value salary variable मध्ये assign झाली.


Assignment flow#

TEXT
Right Side Evaluate
        ↓
      Result
        ↓
Assignment Operator =
        ↓
   Left Variable

Example:

JAVA
int basePrice = 1000;
int tax = 180;

int finalPrice = basePrice + tax;

पहिले:

TEXT
basePrice + tax

evaluate होईल.

TEXT
1000 + 180 → 1180

मग:

TEXT
finalPrice = 1180

Compound Assignment Operators#

Repeated update operations साठी Java compound assignment देते.

OperatorExampleBasic Meaning
+=x += 5add and assign
-=x -= 5subtract and assign
*=x *= 5multiply and assign
/=x /= 5divide and assign
%=x %= 5remainder and assign

+=#

JAVA
int wallet = 1000;

wallet += 500;

System.out.println(wallet);

Output:

TEXT
1500

Conceptually:

JAVA
wallet = wallet + 500;

-=#

Inventory example:

JAVA
int stock = 50;
int soldQuantity = 8;

stock -= soldQuantity;

System.out.println(stock);

Output:

TEXT
42

*=#

JAVA
int points = 100;

points *= 2;

System.out.println(points);

Output:

TEXT
200

/=#

JAVA
int amount = 1000;

amount /= 4;

System.out.println(amount);

Output:

TEXT
250

%=#

JAVA
int value = 17;

value %= 5;

System.out.println(value);

Output:

TEXT
2

Compound Assignment ची एक important Java behavior#

हे दोन्ही नेहमी type-system च्या दृष्टीने identical नसतात:

JAVA
short number = 10;
number += 5;

हे valid आहे.

पण:

JAVA
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:

TEXT
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:

TEXT
true

किंवा:

TEXT
false

असा मिळतो.


Comparison Operators Table#

OperatorMeaning
==equal to
!=not equal to
>greater than
<less than
>=greater than or equal to
<=less than or equal to

Equal To ==#

JAVA
int enteredPin = 1234;
int correctPin = 1234;

boolean matched = enteredPin == correctPin;

System.out.println(matched);

Output:

TEXT
true

Very Important#

= आणि == वेगळे आहेत.

TEXT
=    Assignment
==   Comparison

हा beginner programming मधला अत्यंत common confusion आहे.


Not Equal !=#

JAVA
int currentStock = 10;

boolean outOfStock = currentStock != 0;

System.out.println(outOfStock);

Output:

TEXT
true

Variable च्या नावाकडे लक्ष द्या: या example मध्ये expression technically "stock is not zero" तपासत आहे, त्यामुळे better naming असेल:

JAVA
boolean stockAvailable = currentStock != 0;

Readable code मध्ये variable name आणि condition चा meaning match झाला पाहिजे.


Greater Than >#

JAVA
int score = 82;

boolean abovePassingThreshold = score > 40;

System.out.println(abovePassingThreshold);

Output:

TEXT
true

Less Than <#

JAVA
int availableStock = 3;
int reorderLevel = 5;

boolean needsReorder = availableStock < reorderLevel;

System.out.println(needsReorder);

Output:

TEXT
true

Greater Than or Equal To >=#

Requirement:

User चे age 18 किंवा त्यापेक्षा जास्त असेल तर eligibility true.
JAVA
int age = 18;

boolean eligible = age >= 18;

System.out.println(eligible);

Output:

TEXT
true

जर आपण > वापरला असता:

JAVA
age > 18

तर exactly 18-year-old user incorrectly exclude झाला असता.

हा boundary-condition bug आहे.


Less Than or Equal To <=#

JAVA
int attemptsUsed = 2;
int maximumAttempts = 3;

boolean canContinue = attemptsUsed <= maximumAttempts;

System.out.println(canContinue);

Output:

TEXT
true

Requirement च्या wording वरून operator निवडणे अत्यंत important आहे.


Comparison Chain लिहिता येते का?#

Mathematics मध्ये:

TEXT
1 < x < 10

सहज दिसते.

पण Java मध्ये:

JAVA
boolean result = 1 < x < 10;

हे valid नाही.

का?

पहिली expression:

JAVA
1 < x

याचा result boolean येतो.

नंतर effectively:

TEXT
boolean < 10

असा invalid comparison होईल.

Correct Java expression:

JAVA
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 आहेत:

JAVA
age >= 18

आणि:

JAVA
accountActive

दोन्ही combine करण्यासाठी logical operator लागतो.


Core Logical Operators#

OperatorMeaning
&&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:

LeftRightResult
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Example:

JAVA
int age = 25;
boolean accountActive = true;

boolean eligible = age >= 18 && accountActive;

System.out.println(eligible);

Output:

TEXT
true

दोन्ही conditions true असल्यामुळे final result true.


Logical OR ||#

The || operator produces true when at least one operand is true.

Truth table:

LeftRightResult
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Requirement:

Customer premium member किंवा order amount ₹5000 पेक्षा जास्त असेल तर free delivery.
JAVA
boolean premiumMember = false;
int orderAmount = 6500;

boolean freeDelivery =
        premiumMember || orderAmount >= 5000;

System.out.println(freeDelivery);

Output:

TEXT
true

पहिली condition false असूनही दुसरी true आहे.


Logical NOT !#

The ! operator reverses a boolean value.

TEXT
!true  → false
!false → true

Example:

JAVA
boolean accountBlocked = false;

boolean canLogin = !accountBlocked;

System.out.println(canLogin);

Output:

TEXT
true

Short-Circuit Evaluation#

हा practical Java programming मधला खूप important concept आहे.

&& Short-Circuit#

Java && मध्ये left operand false आला तर right operand evaluate केला जात नाही.

Example:

JAVA
int divisor = 0;

boolean valid =
        divisor != 0 && 100 / divisor > 5;

System.out.println(valid);

Output:

TEXT
false

महत्त्वाचे काय झाले?

पहिली condition:

JAVA
divisor != 0

Result:

TEXT
false

&& ला final result आधीच समजला:

TEXT
false && anything → false

त्यामुळे:

JAVA
100 / divisor

executeच झाले नाही.

म्हणून integer divide-by-zero error टळला.


|| Short-Circuit#

|| मध्ये left operand true असेल तर right operand evaluate करण्याची आवश्यकता नसते.

JAVA
boolean premiumMember = true;
int amount = 1000;

boolean specialAccess =
        premiumMember || amount > 5000;

premiumMember already true असल्यामुळे final OR result true आहे.


Short-Circuit चा mental model#

&&#

TEXT
पहिली condition false?
        ↓
      होय
        ↓
Final result false
        ↓
Right side skip

||#

TEXT
पहिली condition true?
        ↓
      होय
        ↓
Final result true
        ↓
Right side skip

Increment and Decrement Operators#

Increment ++#

The increment operator increases a numeric variable by one.

JAVA
int orderCount = 10;

orderCount++;

System.out.println(orderCount);

Output:

TEXT
11

Equivalent basic idea:

JAVA
orderCount = orderCount + 1;

किंवा:

JAVA
orderCount += 1;

Decrement --#

The decrement operator decreases a numeric variable by one.

JAVA
int remainingAttempts = 3;

remainingAttempts--;

System.out.println(remainingAttempts);

Output:

TEXT
2

Prefix आणि Postfix#

++ आणि -- variable च्या आधी किंवा नंतर येऊ शकतात.

TEXT
++x    Prefix increment
x++    Postfix increment

--x    Prefix decrement
x--    Postfix decrement

Variable standalone statement मध्ये वापरल्यास:

JAVA
x++;

आणि:

JAVA
++x;

दोन्ही शेवटी x एकाने वाढवतात.

Difference तेव्हा स्पष्ट दिसतो जेव्हा expression ला result value म्हणून वापरले जाते.


Postfix Increment x++#

Postfix increment yields the current value first, then increments the variable.

JAVA
int x = 5;

int y = x++;

System.out.println(x);
System.out.println(y);

Output:

TEXT
6
5

Execution:

TEXT
x = 5

y = x++
    ↓
y ला current x → 5

नंतर x increment
    ↓
x = 6

Prefix Increment ++x#

Prefix increment increments the variable first, then yields the updated value.

JAVA
int x = 5;

int y = ++x;

System.out.println(x);
System.out.println(y);

Output:

TEXT
6
6

Execution:

TEXT
x = 5

++x
 ↓
x becomes 6
 ↓
expression value = 6
 ↓
y = 6

Postfix Decrement#

JAVA
int x = 5;

int y = x--;

System.out.println(x);
System.out.println(y);

Output:

TEXT
4
5

Prefix Decrement#

JAVA
int x = 5;

int y = --x;

System.out.println(x);
System.out.println(y);

Output:

TEXT
4
4

Avoid Clever Increment Expressions#

Java technically अशा expressions evaluate करू शकते:

JAVA
int x = 5;

int result = x++ + ++x;

पण production code मध्ये अशी expression unnecessarily confusing आहे.

Step:

TEXT
x = 5

x++ gives 5
x becomes 6

++x makes x 7
++x gives 7

result = 5 + 7
       = 12

Final:

TEXT
x = 7
result = 12

हे समजणे useful आहे.

असे code लिहिणे recommended नाही.

Better approach#

JAVA
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 लागतात:

JAVA
10 + 5

Unary operator ला एक operand लागतो:

JAVA
-10

या chapter मध्ये relevant unary operators:

OperatorPurpose
+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 +#

JAVA
int number = 10;

int result = +number;

System.out.println(result);

Output:

TEXT
10

Unary plus सामान्यतः value चा sign बदलत नाही.


Unary Minus -#

JAVA
int balanceChange = 500;

int reversal = -balanceChange;

System.out.println(reversal);

Output:

TEXT
-500

Important:

JAVA
int number = -10;

इथे - negative literal चा भाग म्हणून दिसत असला तरी language expression level वर unary minus operator म्हणून समजणे useful आहे.


Logical Complement !#

हा देखील unary operator आहे कारण त्याला फक्त एक boolean operand लागतो.

JAVA
boolean paymentFailed = false;

boolean paymentSuccessful = !paymentFailed;

System.out.println(paymentSuccessful);

Output:

TEXT
true

Double Negation#

JAVA
boolean active = true;

System.out.println(!!active);

Output:

TEXT
true

Reason:

TEXT
active        → true
!active       → false
!!active      → true

Production 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:

JAVA
condition ? valueIfTrue : valueIfFalse

Java specification मध्ये याचे official नाव conditional operator ? : आहे. First expression boolean किंवा Boolean असावी आणि condition वरून second किंवा third expression पैकी appropriate expression evaluate होते.


Basic Example#

Requirement:

Marks 40 किंवा जास्त असतील तर "Pass", अन्यथा "Fail".
JAVA
int marks = 65;

String result = marks >= 40 ? "Pass" : "Fail";

System.out.println(result);

Output:

TEXT
Pass

Ternary execution flow#

TEXT
marks >= 40
     ↓
   true?
   /   \
 yes   no
 /       \
"Pass"  "Fail"
   \      /
    result

Ternary returns a value#

हे खूप important आहे.

JAVA
String status =
        marks >= 40 ? "Pass" : "Fail";

पूर्ण ternary expression एक value produce करते.

त्यामुळे आपण ती:

  • variable मध्ये assign करू शकतो
  • expression मध्ये वापरू शकतो
  • method argument म्हणून वापरू शकतो

Numeric Ternary Example#

JAVA
int stock = 7;

int reorderQuantity =
        stock < 10 ? 20 : 0;

System.out.println(reorderQuantity);

Output:

TEXT
20

Boolean Ternary — अनेकदा unnecessary#

Weak:

JAVA
boolean eligible =
        age >= 18 ? true : false;

Better:

JAVA
boolean eligible = age >= 18;

का?

age >= 18 itself already boolean result देते.

Ternary लावून आपण त्याच boolean ला unnecessarily पुन्हा convert करत आहोत.


Nested Ternary#

Technically valid:

JAVA
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:

JAVA
int result = 10 + 5 * 2;

आपण left-to-right blindly केल्यास:

TEXT
10 + 5 = 15
15 * 2 = 30

पण actual result:

TEXT
20

कारण * ची precedence + पेक्षा higher आहे.

Execution:

TEXT
5 * 2 → 10

10 + 10 → 20

Relevant Precedence — High to Low#

PriorityOperators
Higherpostfix x++, x--
unary ++x, --x, +, -, !, ~
*, /, %
+, -
<, <=, >, >=
==, !=
&
^
`\`
&&
`\\`
?:
Lower=, +=, -=, *=, /=, %=

Parentheses वापरणे#

तुम्हाला precedence पाठ असली तरी complex business expression मध्ये parentheses readability improve करतात.

Without explicit grouping:

JAVA
boolean eligible =
        age >= 18 && score >= 60 || premiumMember;

Java precedence मुळे हे logically:

JAVA
boolean eligible =
        (age >= 18 && score >= 60) || premiumMember;

असे evaluate होते.

पण business requirement कोणती आहे हे readable code मध्ये स्पष्ट असले पाहिजे.

Better:

JAVA
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 एकाने वाढवा.
JAVA
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:

TEXT
Subtotal: 3600
Discount Eligible: true
Discount: 360.0
Shipping: 0.0
Final Amount: 3240.0
Remaining Stock: 7
Order Count: 1

Operators used#

Arithmetic#

JAVA
unitPrice * quantity
subtotal * 0.10
subtotal - discount
amountAfterDiscount + shippingCharge

Comparison#

JAVA
subtotal >= 3000
amountAfterDiscount >= 3000

Logical#

JAVA
premiumCustomer && subtotal >= 3000

Ternary#

JAVA
discountEligible ? subtotal * 0.10 : 0

Compound Assignment#

JAVA
stock -= quantity;

Increment#

JAVA
orderCount++;

हा एक realistic example दाखवतो की operators independent syntax नसून actual business logic तयार करण्यासाठी combine केले जातात.


Requirement → Operator Thinking#

Professional developer म्हणून requirement वाचताना keywords ओळखण्याची habit useful आहे.

Requirement wordingLikely 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 amountcompound assignment
simple either/or value?:

3. Common Mistakes & Misconceptions#

Mistake 1 — = आणि == same समजणे#

Wrong understanding:

TEXT
= means equal

Correct understanding:

TEXT
=  assigns
== compares

Example:

JAVA
int age = 18;
boolean adult = age == 18;

Mistake 2 — 5 / 2 म्हणजे 2.5#

JAVA
System.out.println(5 / 2);

Actual:

TEXT
2

Root Cause#

दोन्ही operands integers आहेत.

Fix#

JAVA
System.out.println(5.0 / 2);

Output:

TEXT
2.5

Mistake 3 — Result double मध्ये ठेवला म्हणजे decimal division होईल#

Wrong:

JAVA
double result = 5 / 2;

Actual:

TEXT
2.0

Better:

JAVA
double result = 5.0 / 2;

Mistake 4 — % म्हणजे percentage#

Programming मध्ये:

TEXT
%

हा operator percentage operator नाही.

तो remainder operator आहे.

JAVA
10 % 3

Result:

TEXT
1

10% discount calculate करण्यासाठी:

JAVA
double discount = amount * 0.10;

Mistake 5 — >= requirement ला > लिहिणे#

Requirement:

Minimum age 18.

Wrong:

JAVA
age > 18

Correct:

JAVA
age >= 18

18 boundary case miss करू नका.


Mistake 6 — Mathematical chained comparison#

Wrong:

JAVA
1 < age < 60

Correct:

JAVA
age > 1 && age < 60

Mistake 7 — && आणि || interchange करणे#

Requirement:

User must be active AND verified.

Wrong:

JAVA
active || verified

हे एका condition true असली तरी access देईल.

Correct:

JAVA
active && verified

Mistake 8 — ! चा meaning चुकीचा घेणे#

JAVA
!loggedIn

याचा अर्थ:

TEXT
not logged in

तो variable change करत नाही.

तो boolean expression चे opposite result produce करतो.


Mistake 9 — Prefix/Postfix difference ignore करणे#

JAVA
int x = 5;
int y = x++;

y = 5.

पण:

JAVA
int x = 5;
int y = ++x;

y = 6.


Mistake 10 — Clever increment expressions#

Avoid:

JAVA
int result = x++ + ++x;

जरी output predict करता येत असला तरी readability खराब होते.


Mistake 11 — Ternary प्रत्येक decision साठी वापरणे#

Readable:

JAVA
String result =
        marks >= 40 ? "Pass" : "Fail";

Hard to maintain:

JAVA
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 करणे#

JAVA
boolean allowed =
        active && verified || admin;

Compiler ला meaning clear आहे.

Human reader ला business grouping लगेच clear नसेल.

Better:

JAVA
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#

JAVA
int price = 850;
int quantity = 4;

int total = price * quantity;

System.out.println(total);

Expected Output:

TEXT
3400

Exercise 2 — Complete Boxes#

Problem#

53 items आहेत. एका box मध्ये 10 items बसतात.

Find:

  • complete boxes
  • remaining items

Hints#

Complete boxes → /

Remaining items → %

Solution#

JAVA
int items = 53;
int boxCapacity = 10;

int completeBoxes = items / boxCapacity;
int remainingItems = items % boxCapacity;

System.out.println(completeBoxes);
System.out.println(remainingItems);

Output:

TEXT
5
3

Exercise 3 — Discount Eligibility#

Requirement#

Customer:

  • premium असला पाहिजे
  • आणि order ₹2500 किंवा जास्त असली पाहिजे

Solution#

JAVA
boolean premium = true;
int amount = 3000;

boolean eligible =
        premium && amount >= 2500;

System.out.println(eligible);

Output:

TEXT
true

Exercise 4 — Free Delivery#

Requirement#

Free delivery मिळेल जर:

  • customer premium असेल
  • OR
  • order amount ₹5000 किंवा जास्त असेल

Solution#

JAVA
boolean premium = false;
int amount = 5200;

boolean freeDelivery =
        premium || amount >= 5000;

System.out.println(freeDelivery);

Output:

TEXT
true

Exercise 5 — Stock Update#

Problem#

Starting stock = 25.

7 items sold.

Constraint#

Compound assignment वापरा.

Solution#

JAVA
int stock = 25;

stock -= 7;

System.out.println(stock);

Output:

TEXT
18

Exercise 6 — Predict Prefix/Postfix#

Attempt करण्यापूर्वी answer पाहू नका.

JAVA
int value = 10;

int first = value++;
int second = ++value;

System.out.println(value);
System.out.println(first);
System.out.println(second);

Solution Reasoning#

Initially:

TEXT
value = 10

value++:

TEXT
first = 10
value = 11

++value:

TEXT
value = 12
second = 12

Output:

TEXT
12
10
12

Exercise 7 — Ternary Status#

Requirement#

Balance 0 पेक्षा जास्त असल्यास "Positive" अन्यथा "Zero or Negative".

Solution#

JAVA
int balance = -20;

String status =
        balance > 0
                ? "Positive"
                : "Zero or Negative";

System.out.println(status);

Output:

TEXT
Zero or Negative

Exercise 8 — Range Check#

Requirement#

Score 0 ते 100 inclusive आहे का ते check करा.

Think#

Inclusive म्हणजे:

TEXT
score >= 0

आणि:

TEXT
score <= 100

Solution#

JAVA
int score = 84;

boolean valid =
        score >= 0 && score <= 100;

System.out.println(valid);

Output:

TEXT
true

5. 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.

JAVA
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.

JAVA
double result = 5.0 / 2;

The result is 2.5.


Q5. What does the % operator do?#

Answer: The % operator returns the remainder after division.

JAVA
10 % 3

produces:

TEXT
1

It 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:

JAVA
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:

JAVA
10 > 5

evaluates to:

TEXT
true

Q8. 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:

JAVA
age >= 18

includes age 18, but:

JAVA
age > 18

does 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.

JAVA
active && verified

requires both conditions.

JAVA
premium || orderAmount >= 5000

requires 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:

JAVA
divisor != 0 && 100 / divisor > 5

If 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.

JAVA
!true

produces false, and:

JAVA
!false

produces true.


Q13. What is the difference between prefix and postfix increment?#

Answer: Both increase the variable by one, but their expression values differ.

Prefix:

JAVA
++x

increments first and then yields the updated value.

Postfix:

JAVA
x++

yields the current value first and then increments the variable.


Q14. What is the output?#

JAVA
int x = 5;
int y = x++;

System.out.println(x);
System.out.println(y);

Answer:

TEXT
6
5

x++ yields 5 first and then increments x to 6.


Q15. What is the output?#

JAVA
int x = 5;
int y = ++x;

System.out.println(x);
System.out.println(y);

Answer:

TEXT
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:

JAVA
-number
!condition
++count
--count

Q17. What is the ternary operator?#

Answer: The ternary operator, formally the conditional operator ?:, selects one of two expressions based on a boolean condition.

Syntax:

JAVA
condition ? valueIfTrue : valueIfFalse

Example:

JAVA
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:

JAVA
10 + 5 * 2

evaluates 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:

JAVA
x > 1 && x < 10

The 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#

TEXT
+   Addition
-   Subtraction
*   Multiplication
/   Division
%   Remainder

Integer operands:

JAVA
5 / 2

2

Floating-point operand:

JAVA
5.0 / 2

2.5


Assignment#

TEXT
=    Assign
+=   Add and assign
-=   Subtract and assign
*=   Multiply and assign
/=   Divide and assign
%=   Remainder and assign

Comparison#

TEXT
==   Equal
!=   Not equal
>    Greater
<    Less
>=   Greater or equal
<=   Less or equal

Result always boolean for the comparisons taught here.


Logical#

TEXT
&&   Both must be true
||   At least one true
!    Reverse boolean

Short-circuit:

TEXT
false && ... → right side skipped
true  || ... → right side skipped

Increment / Decrement#

TEXT
x++   use current value, then increment
++x   increment, then use new value

x--   use current value, then decrement
--x   decrement, then use new value

Ternary#

JAVA
condition ? trueValue : falseValue

Simple value selection साठी useful.


Precedence#

Remember the practical sequence:

TEXT
Unary
  ↓
* / %
  ↓
+ -
  ↓
Comparison
  ↓
Logical
  ↓
Ternary
  ↓
Assignment

Complex 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:

TEXT
Course Price      = ₹2000
Number of Courses = 2
Premium User      = true
Wallet Balance    = ₹5000
Purchase Count    = 4

Rules:

  1. Subtotal = course price × quantity.
  2. Premium user आणि subtotal किमान ₹4000 असेल तर ₹500 discount.
  3. Payable amount = subtotal - discount.
  4. Wallet balance payable amount पेक्षा greater than or equal असेल तर purchase possible.
  5. Purchase possible असेल तर status "Eligible", अन्यथा "Insufficient Balance".
  6. Purchase count एकाने increase झाल्याचे independent update दाखवा.
  7. Wallet deduction हा exercise च्या fixed data वर करा.

Attempt First#

Required operators identify करा:

TEXT
*
&&
>=
-
?:
++
-=

Solution#

JAVA
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:

TEXT
Subtotal: 4000
Discount: 500
Payable: 3500
Status: Eligible
Wallet Balance: 1500
Purchase Count: 5

Reasoning#

TEXT
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