Programming and Java Fundamentals
Operators and Expressions
You are viewing a free preview lesson.
Lesson Overview
Program শুধু data store করে না। Stored data-এর ওপর calculation, comparison এবং decision-making operation চালায়।
উদাহরণ:
- দুটি number যোগ করা
- Product price এবং quantity থেকে total বের করা
- একজন learner-এর age
18বা তার বেশি কি না check করা - Email এবং password দুটিই valid কি না নির্ধারণ করা
- একটি counter-এর value বাড়ানো
- Variable-এর existing value update করা
Java-তে এসব operation করার জন্য operator ব্যবহার করা হয়।
যেমন:
int total = 10 + 20;
এখানে + একটি arithmetic operator।
আরেকটি example:
boolean isAdult = age >= 18;
এখানে >= একটি comparison operator।
এই lesson-এ আমরা শিখব:
- Operator কী
- Operand কী
- Expression কী
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Unary operators
- Increment এবং decrement
- Operator precedence
- Parentheses
- Short-circuit evaluation
- String concatenation
- Integer এবং decimal expressions
- Common operator-related error
Learning Objectives
এই lesson শেষ করার পর আপনি পারবেন:
- Operator, operand এবং expression ব্যাখ্যা করতে
- Arithmetic operator ব্যবহার করে calculation করতে
- Assignment এবং compound assignment operator ব্যবহার করতে
- Comparison operator দিয়ে values compare করতে
- Logical operator ব্যবহার করে multiple condition combine করতে
- Unary operator ব্যবহার করতে
- Increment এবং decrement operator বুঝতে
- Prefix এবং postfix operation-এর পার্থক্য ব্যাখ্যা করতে
- Operator precedence অনুযায়ী expression evaluate করতে
- Parentheses ব্যবহার করে calculation order control করতে
- Short-circuit evaluation বুঝতে
- String concatenation এবং numeric addition আলাদা করতে
- Common operator-related bug শনাক্ত করতে
What Is an Operator?
Operator হলো একটি symbol, যা এক বা একাধিক value-এর ওপর একটি operation চালায়।
Example:
int total = 10 + 20;
এখানে:
10
প্রথম value।
20
দ্বিতীয় value।
+
Operator।
10 + 20
একটি expression।
Result:
30
What Is an Operand?
Operator যে value বা variable-এর ওপর কাজ করে, তাকে operand বলা হয়।
Example:
10 + 20
এখানে:
10প্রথম operand20দ্বিতীয় operand+operator
Variable-ও operand হতে পারে:
firstNumber + secondNumber
এখানে:
firstNumberoperandsecondNumberoperand
What Is an Expression?
Expression হলো value, variable এবং operator-এর একটি combination, যা evaluate হয়ে একটি result তৈরি করে।
Example:
10 + 20
Result:
30
আরেকটি expression:
price * quantity
আরেকটি:
age >= 18
Result হতে পারে:
true
আরেকটি:
isEmailValid && isPasswordValid
Result হতে পারে:
true
অথবা:
false
Statement and Expression
Expression একটি value তৈরি করে।
10 + 20
Statement একটি complete instruction।
int total = 10 + 20;
এখানে:
10 + 20
Expression।
সম্পূর্ণ line:
int total = 10 + 20;
Statement।
Types of Java Operators
এই lesson-এ আমরা প্রধানত শিখব:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Unary operators
- Increment and decrement operators
- Conditional operator-এর basic introduction
Arithmetic Operators
Arithmetic operator numeric calculation-এর জন্য ব্যবহার করা হয়।
Java-এর basic arithmetic operators:
| Operator | কাজ |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Remainder বা modulus |
Addition Operator
Addition operator:
+
Example:
int firstNumber = 10;
int secondNumber = 20;
int total = firstNumber + secondNumber;
System.out.println(total);
Output:
30
Adding More Than Two Values
int mathematicsMark = 80;
int englishMark = 90;
int scienceMark = 85;
int totalMarks =
mathematicsMark
+ englishMark
+ scienceMark;
System.out.println(totalMarks);
Output:
255
Subtraction Operator
Subtraction operator:
-
Example:
int totalAmount = 1000;
int spentAmount = 350;
int remainingAmount =
totalAmount - spentAmount;
System.out.println(remainingAmount);
Output:
650
Multiplication Operator
Multiplication operator:
*
Example:
int productPrice = 500;
int quantity = 3;
int totalPrice =
productPrice * quantity;
System.out.println(totalPrice);
Output:
1500
Division Operator
Division operator:
/
Example:
int total = 20;
int count = 4;
int average = total / count;
System.out.println(average);
Output:
5
Integer Division
দুটি integer divide করলে result-ও integer হয়।
int result = 5 / 2;
System.out.println(result);
Output:
2
Mathematical result:
2.5
কিন্তু Java integer division fractional অংশ বাদ দেয়।
Getting a Decimal Result
কমপক্ষে একটি operand floating-point type হতে হবে।
double result = 5.0 / 2;
System.out.println(result);
Output:
2.5
এটিও valid:
double result = 5 / 2.0;
এটিও:
double result = 5.0 / 2.0;
Common Average Bug
Wrong:
int totalMarks = 250;
int subjectCount = 3;
double average =
totalMarks / subjectCount;
System.out.println(average);
Output:
83.0
Expected mathematical result প্রায়:
83.3333...
কারণ division আগে integer arithmetic হিসেবে হয়েছে।
Correct:
double average =
totalMarks / (double) subjectCount;
অথবা:
double average =
totalMarks / 3.0;
Division by Zero
Integer division by zero runtime error তৈরি করে।
int result = 10 / 0;
Possible exception:
ArithmeticException
Floating-Point Division by Zero
double result = 10.0 / 0.0;
System.out.println(result);
Output:
Infinity
আরেকটি:
double result = 0.0 / 0.0;
Output:
NaN
NaN মানে:
Not a Number
Remainder Operator
Remainder operator:
%
এটি division-এর পরে remainder return করে।
Example:
int remainder = 10 % 3;
System.out.println(remainder);
Output:
1
কারণ:
10 ÷ 3 = 3
Remainder = 1
Checking Even and Odd Numbers
একটি number even হলে 2 দিয়ে divide করার remainder 0।
int number = 10;
boolean isEven =
number % 2 == 0;
System.out.println(isEven);
Output:
true
Odd check:
boolean isOdd =
number % 2 != 0;
Other Uses of the Remainder Operator
% ব্যবহার করা যায়:
- Even বা odd check
- Cycle repeat করা
- Time conversion
- Pagination calculation
- Alternating behaviour
- Digit extraction
Example:
int seconds = 125;
int remainingSeconds =
seconds % 60;
System.out.println(remainingSeconds);
Output:
5
Arithmetic with Negative Values
int firstValue = -10;
int secondValue = 4;
System.out.println(firstValue + secondValue);
System.out.println(firstValue - secondValue);
System.out.println(firstValue * secondValue);
System.out.println(firstValue / secondValue);
System.out.println(firstValue % secondValue);
Output:
-6
-14
-40
-2
-2
Java integer division result zero-এর দিকে truncate করে।
Arithmetic Type Promotion
Different numeric type-এর মধ্যে calculation হলে result wider type-এ promote হতে পারে।
int quantity = 3;
double price = 99.50;
double total = quantity * price;
System.out.println(total);
Output:
298.5
double wider type হওয়ায় result double।
Integer Arithmetic May Overflow Before Assignment
Wrong:
long result =
2_000_000_000
+ 2_000_000_000;
System.out.println(result);
Operands দুটি int হওয়ায় addition আগে int হিসেবে হতে পারে এবং overflow হতে পারে।
Correct:
long result =
2_000_000_000L
+ 2_000_000_000L;
System.out.println(result);
Output:
4000000000
Assignment Operator
Basic assignment operator:
=
Example:
int age = 30;
ডান পাশের value বাম পাশের variable-এ assign হয়।
30 → age
Reassignment
int score = 80;
score = 90;
System.out.println(score);
Output:
90
Assignment Is Right-to-Left
int firstValue;
int secondValue;
firstValue = secondValue = 10;
Evaluation:
secondValue = 10
firstValue = secondValue
Final values:
firstValue = 10
secondValue = 10
যদিও এটি valid, readability-এর জন্য separate statements ভালো।
int firstValue = 10;
int secondValue = 10;
Compound Assignment Operators
Compound assignment operator existing value-এর সঙ্গে operation চালিয়ে result আবার একই variable-এ assign করে।
| Operator | Equivalent |
|---|---|
+= | value = value + ... |
-= | value = value - ... |
*= | value = value * ... |
/= | value = value / ... |
%= | value = value % ... |
Addition Assignment
Long form:
int score = 10;
score = score + 5;
Short form:
int score = 10;
score += 5;
Final value:
15
Subtraction Assignment
int balance = 1000;
balance -= 250;
System.out.println(balance);
Output:
750
Equivalent:
balance = balance - 250;
Multiplication Assignment
int value = 10;
value *= 3;
System.out.println(value);
Output:
30
Division Assignment
int value = 20;
value /= 4;
System.out.println(value);
Output:
5
Remainder Assignment
int value = 17;
value %= 5;
System.out.println(value);
Output:
2
Compound Assignment and Type Conversion
Compound assignment কিছু implicit conversion করতে পারে।
Example:
byte value = 10;
value += 5;
এটি compile করতে পারে।
কিন্তু:
byte value = 10;
value = value + 5;
এটি compile নাও করতে পারে, কারণ byte + int result int।
Equivalent behaviour conceptually:
value = (byte) (value + 5);
এই কারণে compound assignment convenient হলেও silent narrowing conversion-এর বিষয়ে সতর্ক থাকা উচিত।
Comparison Operators
Comparison operator দুটি value compare করে এবং একটি boolean result দেয়।
Java comparison operators:
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Equality Operator
Equality operator:
==
Example:
int age = 18;
boolean isExactlyEighteen =
age == 18;
System.out.println(isExactlyEighteen);
Output:
true
Assignment vs Equality
Assignment:
age = 18;
Comparison:
age == 18
একটি = value assign করে।
দুটি == equality compare করে।
Not Equal Operator
!=
Example:
int score = 80;
boolean isNotPerfect =
score != 100;
System.out.println(isNotPerfect);
Output:
true
Greater Than Operator
>
Example:
int score = 80;
boolean passedWithHighScore =
score > 75;
Result:
true
Less Than Operator
<
Example:
int stock = 5;
boolean lowStock =
stock < 10;
Result:
true
Greater Than or Equal To
>=
Example:
int age = 18;
boolean isAdult =
age >= 18;
Result:
true
Less Than or Equal To
<=
Example:
int score = 40;
boolean needsImprovement =
score <= 40;
Result:
true
Range Checking
ধরা যাক valid mark range:
0 to 100
int mark = 85;
boolean validMark =
mark >= 0
&& mark <= 100;
Result:
true
এখানে logical && operator ব্যবহার করা হয়েছে, যা আমরা পরে শিখব।
Comparing Characters
char values Unicode numeric value অনুযায়ী compare করা যায়।
char firstLetter = 'A';
char secondLetter = 'B';
System.out.println(firstLetter < secondLetter);
Output:
true
কারণ 'A'-এর numeric value 'B'-এর চেয়ে ছোট।
Comparing Floating-Point Values
Direct equality risky হতে পারে।
double result = 0.1 + 0.2;
System.out.println(result == 0.3);
Output হতে পারে:
false
Approximate comparison:
double expected = 0.3;
double tolerance = 0.000001;
boolean approximatelyEqual =
Math.abs(result - expected)
< tolerance;
Comparing Strings
String content compare করতে == ব্যবহার করা উচিত নয়।
Wrong:
String firstValue =
new String("Java");
String secondValue =
new String("Java");
System.out.println(
firstValue == secondValue
);
== reference identity compare করে।
Correct:
System.out.println(
firstValue.equals(secondValue)
);
Logical Operators
Logical operator boolean expression combine বা reverse করতে ব্যবহৃত হয়।
Java-এর common logical operators:
| Operator | Meaning |
|---|---|
&& | Logical AND |
| ` | |
! | Logical NOT |
Logical AND
AND operator:
&&
Result true হবে যখন দুই পাশের condition-ই true।
Truth table:
| Left | Right | Result |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
AND Example
boolean emailValid = true;
boolean passwordValid = true;
boolean canLogin =
emailValid && passwordValid;
System.out.println(canLogin);
Output:
true
যদি password invalid হয়:
boolean emailValid = true;
boolean passwordValid = false;
Result:
false
Age Range with AND
int age = 25;
boolean withinRange =
age >= 18
&& age <= 60;
এখানে দুই condition-ই true হতে হবে।
Logical OR
OR operator:
||
কমপক্ষে একটি condition true হলে result true।
Truth table:
| Left | Right | Result |
|---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
OR Example
boolean isAdmin = false;
boolean isInstructor = true;
boolean canAccessHarbor =
isAdmin || isInstructor;
System.out.println(canAccessHarbor);
Output:
true
Multiple Allowed Roles
boolean isInstructor = false;
boolean isTeamMember = true;
boolean isAdmin = false;
boolean hasAccess =
isInstructor
|| isTeamMember
|| isAdmin;
Result:
true
Logical NOT
NOT operator:
!
Boolean value reverse করে।
boolean active = true;
boolean inactive = !active;
System.out.println(inactive);
Output:
false
NOT Example
boolean enrollmentClosed = false;
if (!enrollmentClosed) {
System.out.println("Enrollment is open");
}
!enrollmentClosed result true।
Combining Logical Operators
int age = 20;
boolean emailVerified = true;
boolean accountBlocked = false;
boolean canEnroll =
age >= 18
&& emailVerified
&& !accountBlocked;
canEnroll true হবে যদি:
- Age অন্তত
18 - Email verified
- Account blocked নয়
Use Parentheses for Clarity
boolean allowed =
isAdmin
|| isInstructor && isActive;
Operator precedence অনুযায়ী && আগে evaluate হবে।
Equivalent:
boolean allowed =
isAdmin
|| (isInstructor && isActive);
কিন্তু যদি requirement হয় admin বা instructor—দুজনকেই active হতে হবে:
boolean allowed =
(isAdmin || isInstructor)
&& isActive;
Parentheses logic পরিষ্কার করে।
Short-Circuit Evaluation
&& এবং || short-circuit operator।
এর অর্থ, result আগেই জানা গেলে Java দ্বিতীয় operand evaluate নাও করতে পারে।
Short-Circuit AND
leftCondition && rightCondition
যদি leftCondition false হয়, পুরো expression false।
তাই Java right side evaluate করে না।
Example:
String value = null;
boolean valid =
value != null
&& !value.isBlank();
যদি value == null, প্রথম condition false।
তাই:
value.isBlank()
call হবে না।
এতে NullPointerException avoid হয়।
Wrong Condition Order
Risky:
boolean valid =
!value.isBlank()
&& value != null;
এখানে value null হলে প্রথমেই method call হবে এবং error হবে।
Correct:
boolean valid =
value != null
&& !value.isBlank();
Short-Circuit OR
leftCondition || rightCondition
যদি left side true হয়, পুরো expression true।
Right side evaluate হয় না।
Example:
boolean hasAccess =
isAdmin
|| checkInstructorPermission();
যদি isAdmin true হয়, method call নাও হতে পারে।
& and | with Booleans
Java-তে single:
&
|
boolean operand-এর সঙ্গেও ব্যবহার করা যায়।
Difference:
&&short-circuit AND&দুই side-ই evaluate করে||short-circuit OR|দুই side-ই evaluate করে
Example:
boolean result =
firstCondition & secondCondition;
Business condition-এর জন্য সাধারণত && এবং || ব্যবহার করা উচিত।
Single & এবং | bitwise operation-এও ব্যবহৃত হয়, যা advanced topic।
Unary Operators
Unary operator একটি operand-এর ওপর কাজ করে।
Common unary operators:
| Operator | Meaning |
|---|---|
+ | Positive value |
- | Negative value |
! | Boolean negation |
++ | Increment |
-- | Decrement |
Unary Plus
int value = +10;
Result:
10
Unary plus খুব কম প্রয়োজন হয়।
Unary Minus
int value = 10;
int negativeValue = -value;
System.out.println(negativeValue);
Output:
-10
Negating a Negative Value
int value = -10;
int positiveValue = -value;
System.out.println(positiveValue);
Output:
10
Logical Negation
boolean active = true;
System.out.println(!active);
Output:
false
Double negation:
System.out.println(!!active);
Output:
true
Double negation সাধারণ Java code-এ rarely needed।
Increment Operator
Increment operator:
++
Variable-এর value 1 বাড়ায়।
int count = 10;
count++;
System.out.println(count);
Output:
11
Equivalent:
count = count + 1;
অথবা:
count += 1;
Decrement Operator
Decrement operator:
--
Variable-এর value 1 কমায়।
int count = 10;
count--;
System.out.println(count);
Output:
9
Prefix Increment
++count
প্রথমে value increment হয়, তারপর expression-এ নতুন value ব্যবহৃত হয়।
int count = 10;
int result = ++count;
System.out.println(count);
System.out.println(result);
Output:
11
11
Postfix Increment
count++
Expression-এ প্রথমে পুরোনো value ব্যবহৃত হয়, তারপর variable increment হয়।
int count = 10;
int result = count++;
System.out.println(count);
System.out.println(result);
Output:
11
10
Prefix vs Postfix
Prefix
int value = 5;
int result = ++value;
Step:
valuebecomes6resultgets6
Postfix
int value = 5;
int result = value++;
Step:
resultgets5valuebecomes6
Prefix Decrement
int value = 5;
int result = --value;
Final:
value = 4
result = 4
Postfix Decrement
int value = 5;
int result = value--;
Final:
value = 4
result = 5
Avoid Complex Increment Expressions
Technically valid:
int result =
count++ + ++count;
কিন্তু code খুব confusing।
Avoid করুন।
Better:
count++;
count++;
int result = previousValue + count;
অথবা logic আরও পরিষ্কারভাবে rewrite করুন।
Increment এবং decrement simple standalone statement হিসেবে ব্যবহার করা ভালো।
count++;
Increment Works Only on Variables
Invalid:
10++;
কারণ literal update করা যায় না।
Correct:
int value = 10;
value++;
Operator Precedence
একটি expression-এ multiple operator থাকলে কোনটি আগে evaluate হবে, তা operator precedence নির্ধারণ করে।
Example:
int result = 10 + 5 * 2;
Multiplication-এর precedence addition-এর চেয়ে বেশি।
তাই:
5 × 2 = 10
10 + 10 = 20
Output:
20
Using Parentheses
int result = (10 + 5) * 2;
Parentheses-এর expression আগে evaluate হয়।
10 + 5 = 15
15 × 2 = 30
Output:
30
Common Precedence Order
Simplified high-to-low order:
- Parentheses
- Unary operators
- Multiplication, division, remainder
- Addition, subtraction
- Relational operators
- Equality operators
- Logical AND
- Logical OR
- Assignment
Example symbols:
()
++ -- ! + -
* / %
+ -
< <= > >=
== !=
&&
||
= += -= *= /= %=
Multiplication and Division Have Equal Precedence
Equal-precedence operators সাধারণত left-to-right evaluate হয়।
int result = 20 / 5 * 2;
Step:
20 / 5 = 4
4 * 2 = 8
Output:
8
Addition and Subtraction Left-to-Right
int result = 10 - 5 + 2;
Step:
10 - 5 = 5
5 + 2 = 7
Output:
7
Division Order Matters
int firstResult = 20 / 5 / 2;
Step:
20 / 5 = 4
4 / 2 = 2
Output:
2
কিন্তু:
int secondResult = 20 / (5 / 2);
5 / 2 integer division:
2
তারপর:
20 / 2 = 10
Output:
10
Comparison and Logical Precedence
boolean result =
age >= 18
&& score >= 40;
Comparison আগে evaluate হয়:
age >= 18
score >= 40
তারপর:
result1 && result2
Parentheses Improve Readability
Precedence জানা থাকলেও complex condition-এ parentheses ব্যবহার করুন।
Less clear:
boolean allowed =
isAdmin
|| isInstructor
&& isActive;
Clearer:
boolean allowed =
isAdmin
|| (isInstructor && isActive);
String Concatenation Operator
+ numeric addition এবং String concatenation—দুই কাজেই ব্যবহার হয়।
Numeric:
int result = 10 + 20;
Output:
30
String:
String result = "Java" + " Foundation";
Output:
Java Foundation
Mixed String and Number Expressions
System.out.println("Total: " + 10 + 20);
Output:
Total: 1020
কারণ expression left-to-right evaluate হয়।
Correct Numeric Calculation
System.out.println(
"Total: " + (10 + 20)
);
Output:
Total: 30
More Concatenation Examples
System.out.println(10 + 20 + " Total");
Output:
30 Total
কারণ first operation numeric addition।
System.out.println("Total " + 10 + 20);
Output:
Total 1020
কারণ first operation String concatenation।
Compound String Assignment
String message = "Hello";
message += " Java";
System.out.println(message);
Output:
Hello Java
Equivalent:
message = message + " Java";
String immutable হলেও variable নতুন String reference করতে পারে।
Conditional Operator
Conditional বা ternary operator:
condition ? valueIfTrue : valueIfFalse
Example:
int age = 20;
String status =
age >= 18
? "Adult"
: "Minor";
System.out.println(status);
Output:
Adult
Ternary Operator Structure
condition
? resultWhenTrue
: resultWhenFalse;
Example:
int score = 75;
String result =
score >= 40
? "Passed"
: "Failed";
When to Use Ternary
Ternary useful যখন:
- Simple condition
- একটি value choose করতে হবে
- Expression concise থাকে
Good:
String status =
active ? "Active" : "Inactive";
Avoid deeply nested ternary:
String grade =
score >= 80 ? "A"
: score >= 70 ? "B"
: score >= 60 ? "C"
: "F";
এটি technically valid, কিন্তু readability কমতে পারে।
Complex decision-এর জন্য if-else ভালো।
Expressions with Different Types
int quantity = 3;
double price = 99.50;
double total =
quantity * price;
Result double।
Character Arithmetic
char letter = 'A';
int nextCode = letter + 1;
System.out.println(nextCode);
Output:
66
Character arithmetic-এর result সাধারণত int।
Character পেতে casting প্রয়োজন:
char nextLetter =
(char) (letter + 1);
System.out.println(nextLetter);
Output:
B
Boolean Expressions
যে expression true বা false return করে, সেটি boolean expression।
Examples:
age >= 18
score == 100
courseName.equals("Java")
isActive && hasPermission
!accountBlocked
Condition statement-এ boolean expression ব্যবহার করা হয়।
De Morgan's Laws
Complex logical expression simplify করতে দুটি useful rule আছে।
!(A && B) == !A || !B
এবং:
!(A || B) == !A && !B
Example:
boolean notBothValid =
!(emailValid && passwordValid);
Equivalent:
boolean notBothValid =
!emailValid || !passwordValid;
Beginner হিসেবে complex negation-এ parentheses carefully ব্যবহার করুন।
Avoid Double Negatives
Hard to read:
boolean allowed =
!isNotActive;
Better variable design:
boolean isActive;
Then:
boolean allowed = isActive;
Boolean name positive এবং clear রাখার চেষ্টা করুন।
Practical Example: Product Order
public class Main {
static final int DELIVERY_CHARGE = 80;
public static void main(String[] args) {
int productPrice = 500;
int quantity = 3;
int subtotal =
productPrice * quantity;
int total =
subtotal + DELIVERY_CHARGE;
boolean freeDelivery =
subtotal >= 2000;
System.out.println(
"Subtotal: " + subtotal
);
System.out.println(
"Delivery charge: "
+ DELIVERY_CHARGE
);
System.out.println(
"Total: " + total
);
System.out.println(
"Free delivery eligible: "
+ freeDelivery
);
}
}
Output:
Subtotal: 1500
Delivery charge: 80
Total: 1580
Free delivery eligible: false
Practical Example: Student Result
public class Main {
public static void main(String[] args) {
int mathematicsMark = 80;
int englishMark = 90;
int scienceMark = 85;
int totalMarks =
mathematicsMark
+ englishMark
+ scienceMark;
double averageMark =
totalMarks / 3.0;
boolean passedAllSubjects =
mathematicsMark >= 40
&& englishMark >= 40
&& scienceMark >= 40;
String result =
passedAllSubjects
? "Passed"
: "Failed";
System.out.println(
"Total: " + totalMarks
);
System.out.println(
"Average: " + averageMark
);
System.out.println(
"Result: " + result
);
}
}
Output:
Total: 255
Average: 85.0
Result: Passed
Practical Example: Enrollment Eligibility
public class Main {
static final int MINIMUM_AGE = 18;
public static void main(String[] args) {
int learnerAge = 20;
boolean emailVerified = true;
boolean accountBlocked = false;
boolean enrollmentOpen = true;
boolean canEnroll =
learnerAge >= MINIMUM_AGE
&& emailVerified
&& !accountBlocked
&& enrollmentOpen;
System.out.println(
"Can enroll: " + canEnroll
);
}
}
Output:
Can enroll: true
Practical Example: Login Permission
public class Main {
public static void main(String[] args) {
boolean emailValid = true;
boolean passwordValid = true;
boolean accountLocked = false;
boolean canLogin =
emailValid
&& passwordValid
&& !accountLocked;
System.out.println(
"Login allowed: " + canLogin
);
}
}
Practical Example: Discount Calculation
public class Main {
public static void main(String[] args) {
int orderTotal = 5000;
boolean premiumCustomer = true;
boolean eligibleForDiscount =
orderTotal >= 5000
|| premiumCustomer;
int discount =
eligibleForDiscount
? 500
: 0;
int finalTotal =
orderTotal - discount;
System.out.println(
"Discount: " + discount
);
System.out.println(
"Final total: " + finalTotal
);
}
}
Common Error: Assignment Instead of Comparison
Wrong intention:
boolean active = false;
// active = true assigns a value
Condition-এর মধ্যে boolean assignment technically compile করতে পারে:
if (active = true) {
System.out.println("Active");
}
এখানে comparison হয়নি। true assign হয়েছে।
Better:
if (active) {
System.out.println("Active");
}
অথবা explicit:
if (active == true) {
System.out.println("Active");
}
প্রথম version বেশি idiomatic।
Common Error: Comparing Boolean with true
Verbose:
if (isAvailable == true) {
}
Better:
if (isAvailable) {
}
For false:
if (!isAvailable) {
}
Common Error: Integer Division
Wrong:
double average = 5 / 2;
Result:
2.0
Correct:
double average = 5.0 / 2;
Common Error: String Concatenation
Wrong expectation:
System.out.println(
"Total: " + 10 + 20
);
Output:
Total: 1020
Correct:
System.out.println(
"Total: " + (10 + 20)
);
Common Error: Direct Double Equality
Risky:
double result = 0.1 + 0.2;
if (result == 0.3) {
}
Use tolerance:
boolean equal =
Math.abs(result - 0.3)
< 0.000001;
Common Error: Wrong Logical Operator
Requirement:
Email এবং password দুটিই valid হতে হবে।
Wrong:
boolean canLogin =
emailValid || passwordValid;
এতে একটি valid হলেই login allowed হবে।
Correct:
boolean canLogin =
emailValid && passwordValid;
Common Error: Incorrect Range Logic
Requirement:
Mark must be between 0 and 100
Wrong:
boolean valid =
mark >= 0 || mark <= 100;
এটি প্রায় সব number-এর জন্য true হতে পারে।
Correct:
boolean valid =
mark >= 0 && mark <= 100;
Common Error: Null Check Order
Wrong:
boolean valid =
!name.isBlank()
&& name != null;
name null হলে error।
Correct:
boolean valid =
name != null
&& !name.isBlank();
Common Error: Increment in Complex Expression
Confusing:
int count = 5;
int result =
count++ + ++count;
এই code বুঝতে কঠিন এবং bug-prone।
Prefer separate statements।
Common Error: Overflow Before long Assignment
Wrong:
long result =
2_000_000_000
* 2;
Multiplication int হিসেবে overflow করতে পারে।
Correct:
long result =
2_000_000_000L
* 2;
Common Error: Extra Semicolon after if
Wrong:
if (age >= 18);
{
System.out.println("Adult");
}
if statement extra semicolon-এ শেষ হয়েছে।
Block সবসময় execute করবে।
Correct:
if (age >= 18) {
System.out.println("Adult");
}
Common Error: Chained Comparison
Mathematical style:
0 <= mark <= 100
Java-তে invalid:
boolean valid =
0 <= mark <= 100;
Correct:
boolean valid =
mark >= 0
&& mark <= 100;
Common Error: Confusing ! and !=
!
Boolean negation।
!active
!=
Not equal comparison।
score != 100
এগুলো আলাদা।
Common Error: Using & Instead of &&
boolean valid =
value != null
& !value.isBlank();
Single & দুই side evaluate করবে।
value null হলে second side error করবে।
Correct:
boolean valid =
value != null
&& !value.isBlank();
Debugging Expressions
Expression debug করার সময় intermediate result variable-এ store করুন।
Hard to inspect:
boolean canEnroll =
age >= 18
&& emailVerified
&& !accountBlocked
&& enrollmentOpen;
Debug version:
boolean ageValid =
age >= 18;
boolean accountAllowed =
!accountBlocked;
System.out.println(
"ageValid = " + ageValid
);
System.out.println(
"emailVerified = "
+ emailVerified
);
System.out.println(
"accountAllowed = "
+ accountAllowed
);
System.out.println(
"enrollmentOpen = "
+ enrollmentOpen
);
Intermediate value bug identify করতে সাহায্য করে।
Important Terms
Operator
এক বা একাধিক value-এর ওপর operation চালানো symbol।
Operand
Operator যে value বা variable-এর ওপর কাজ করে।
Expression
Operator, variable এবং value-এর combination, যা result তৈরি করে।
Arithmetic Operator
Numeric calculation-এর operator।
Assignment Operator
Variable-এ value assign করে।
Compound Assignment
Operation এবং assignment একসঙ্গে করে।
Comparison Operator
দুটি value compare করে boolean result দেয়।
Logical Operator
একাধিক boolean condition combine বা reverse করে।
Unary Operator
একটি operand-এর ওপর কাজ করে।
Increment
Value 1 বাড়ানো।
Decrement
Value 1 কমানো।
Prefix
Value আগে update হয়, তারপর expression-এ ব্যবহৃত হয়।
Postfix
Expression-এ পুরোনো value ব্যবহৃত হয়, তারপর update হয়।
Operator Precedence
Multiple operator-এর evaluation order।
Short-Circuit Evaluation
Result আগে জানা গেলে remaining expression evaluate না করা।
Ternary Operator
Condition অনুযায়ী দুটি value-এর মধ্যে একটি select করে।
Remainder
Division-এর পরে অবশিষ্ট value।
Practice Exercise 1: Identify Operators and Operands
Expression:
price * quantity
Identify করুন:
- Operator
- First operand
- Second operand
- Expression-এর possible result type
Practice Exercise 2: Arithmetic Operations
Variables:
int firstNumber = 20;
int secondNumber = 6;
নিচের result বের করুন:
- Addition
- Subtraction
- Multiplication
- Integer division
- Remainder
Practice Exercise 3: Predict the Output
System.out.println(10 + 5 * 2);
System.out.println((10 + 5) * 2);
System.out.println(20 / 5 * 2);
System.out.println(20 / (5 * 2));
Practice Exercise 4: Integer Division
Output predict করুন:
int firstResult = 7 / 2;
double secondResult = 7 / 2;
double thirdResult = 7.0 / 2;
double fourthResult = 7 / 2.0;
Practice Exercise 5: Even or Odd
একটি int variable তৈরি করুন।
% operator ব্যবহার করে determine করুন numberটি even কি না।
Example:
Number: 12
Even: true
Practice Exercise 6: Compound Assignment
নিচের code short form-এ লিখুন:
score = score + 10;
balance = balance - 500;
price = price * 2;
total = total / 4;
value = value % 3;
Practice Exercise 7: Trace the Variable
int value = 10;
value += 5;
value *= 2;
value -= 4;
value /= 2;
প্রতিটি step-এর পরে value লিখুন।
Practice Exercise 8: Comparison Results
Variables:
int firstValue = 10;
int secondValue = 20;
Output predict করুন:
System.out.println(firstValue == secondValue);
System.out.println(firstValue != secondValue);
System.out.println(firstValue > secondValue);
System.out.println(firstValue < secondValue);
System.out.println(firstValue >= 10);
System.out.println(secondValue <= 20);
Practice Exercise 9: Valid Mark Range
একটি mark valid হবে যখন:
0 <= mark <= 100
Java boolean expression লিখুন।
Practice Exercise 10: Login Validation
Variables:
boolean emailValid = true;
boolean passwordValid = false;
canLogin determine করুন।
Requirement:
Email এবং password দুটিই valid হতে হবে।
Practice Exercise 11: Role Access
Variables:
boolean isInstructor = false;
boolean isTeamMember = true;
boolean isAdmin = false;
Requirement:
যেকোনো একটি role থাকলে access দেওয়া হবে।
Boolean expression লিখুন।
Practice Exercise 12: Enrollment Eligibility
Requirements:
- Age at least
18 - Email verified
- Account blocked নয়
- Enrollment open
Variables তৈরি করে canEnroll expression লিখুন।
Practice Exercise 13: Short-Circuit Safety
নিচের code safe কি না ব্যাখ্যা করুন:
String name = null;
boolean valid =
name != null
&& !name.isBlank();
তারপর operator order উল্টালে কী হবে, তা লিখুন।
Practice Exercise 14: Prefix vs Postfix
Output predict করুন:
int firstValue = 5;
int firstResult = ++firstValue;
int secondValue = 5;
int secondResult = secondValue++;
System.out.println(firstValue);
System.out.println(firstResult);
System.out.println(secondValue);
System.out.println(secondResult);
Practice Exercise 15: Increment Counter
একটি variable:
int lessonProgress = 0;
তিনবার increment করুন।
Expected final output:
3
Practice Exercise 16: String Concatenation
Output predict করুন:
System.out.println("Total: " + 10 + 20);
System.out.println("Total: " + (10 + 20));
System.out.println(10 + 20 + " Total");
System.out.println("Result " + 10 * 2);
Practice Exercise 17: Ternary Operator
Variable:
int score = 75;
Ternary operator ব্যবহার করে:
Passed
অথবা:
Failed
return করুন।
Passing mark:
40
Practice Exercise 18: Fix the Range Logic
Wrong:
boolean valid =
mark >= 0 || mark <= 100;
Codeটি fix করুন এবং explain করুন কেন || ভুল ছিল।
Practice Exercise 19: Fix the Addition
Wrong output:
System.out.println(
"Average: " + 80 + 90 + 85
);
Expected:
Average: 255
Code fix করুন।
Practice Exercise 20: Prevent Overflow
Wrong:
long total =
2_000_000_000
+ 2_000_000_000;
Expected:
4000000000
Code fix করুন।
Practice Exercise 21: Product Order Program
Create variables:
productPrice
quantity
deliveryCharge
subtotal
total
Formula:
subtotal = productPrice × quantity
total = subtotal + deliveryCharge
Output format:
Product price: 500
Quantity: 3
Subtotal: 1500
Delivery charge: 80
Total: 1580
Practice Exercise 22: Student Result Program
Use:
Mathematics: 80
English: 90
Science: 85
Calculate:
- Total
- Decimal average
- Passed all subjects কি না
- Ternary operator দিয়ে final result
Passing mark per subject:
40
Practice Exercise 23: Discount Eligibility
Discount পাওয়া যাবে যদি:
- Order total অন্তত
5000 - অথবা customer premium
Variables:
int orderTotal = 4500;
boolean premiumCustomer = true;
Determine করুন customer eligible কি না।
Practice Exercise 24: Fix the Null Check
Wrong:
String courseName = null;
boolean valid =
!courseName.isBlank()
&& courseName != null;
Safe version লিখুন।
Practice Exercise 25: Explain in Your Own Words
নিজের ভাষায় উত্তর দিন:
- Operator কী?
- Operand কী?
- Expression কী?
- Integer division কী?
%operator কী করে?- Assignment এবং equality operator-এর পার্থক্য কী?
- Compound assignment কী?
- Comparison operator কী return করে?
&&এবং||-এর পার্থক্য কী?!operator কী করে?- Short-circuit evaluation কী?
- Prefix এবং postfix increment-এর পার্থক্য কী?
- Operator precedence কী?
- Parentheses কেন ব্যবহার করা হয়?
- String concatenation-এ parentheses কখন প্রয়োজন?
- Ternary operator কখন useful?
doublecompare করতে direct==risky কেন?&এবং&&-এর পার্থক্য কী?
Knowledge Check
Question 1
Operator কী?
Question 2
Operand কী?
Question 3
Expression কী?
Question 4
Java-এর basic arithmetic operators কী কী?
Question 5
% operator কী return করে?
Question 6
5 / 2-এর result কী?
Question 7
Decimal result পাওয়ার জন্য কী করতে হবে?
Question 8
Assignment operator কোনটি?
Question 9
Equality operator কোনটি?
Question 10
Compound addition assignment কোনটি?
Question 11
Comparison operator কী ধরনের result দেয়?
Question 12
Logical AND operator কোনটি?
Question 13
Logical OR operator কোনটি?
Question 14
Logical NOT operator কোনটি?
Question 15
&& কখন true হয়?
Question 16
|| কখন true হয়?
Question 17
Short-circuit AND কীভাবে কাজ করে?
Question 18
Prefix increment কী?
Question 19
Postfix increment কী?
Question 20
10 + 5 * 2-এর result কী?
Question 21
(10 + 5) * 2-এর result কী?
Question 22
String content compare করতে কোন method ব্যবহার করা উচিত?
Question 23
"Total: " + 10 + 20-এর output কী?
Question 24
Ternary operator-এর syntax কী?
Question 25
value != null && !value.isBlank() safe কেন?
Question 26
& এবং &&-এর প্রধান পার্থক্য কী?
Question 27
byte + byte expression সাধারণত কোন type return করে?
Question 28
Integer overflow assignment-এর আগে ঘটতে পারে কি?
Question 29
Direct floating-point equality risky কেন?
Question 30
Complex expression readable করতে কী ব্যবহার করা উচিত?
Knowledge Check Answers
Answer 1
Operator হলো এমন symbol, যা এক বা একাধিক value-এর ওপর operation চালায়।
Answer 2
Operator যে value বা variable-এর ওপর কাজ করে, সেটি operand।
Answer 3
Value, variable এবং operator-এর combination, যা evaluate হয়ে result দেয়, সেটি expression।
Answer 4
+
-
*
/
%
Answer 5
Division-এর remainder return করে।
Answer 6
2
কারণ integer division।
Answer 7
কমপক্ষে একটি operand floating-point করতে হবে।
Example:
5.0 / 2
Answer 8
=
Answer 9
==
Answer 10
+=
Answer 11
boolean result দেয়।
Answer 12
&&
Answer 13
||
Answer 14
!
Answer 15
দুই condition-ই true হলে।
Answer 16
কমপক্ষে একটি condition true হলে।
Answer 17
Left side false হলে right side evaluate হয় না।
Answer 18
Prefix increment আগে value বাড়ায়, তারপর updated value expression-এ ব্যবহার করে।
++value
Answer 19
Postfix increment আগে current value expression-এ ব্যবহার করে, তারপর value বাড়ায়।
value++
Answer 20
20
Answer 21
30
Answer 22
equals()
Answer 23
Total: 1020
Answer 24
condition ? valueIfTrue : valueIfFalse
Answer 25
value null হলে first condition false হয় এবং short-circuit-এর কারণে isBlank() call হয় না।
Answer 26
&& short-circuit করে। & দুই side-ই evaluate করে।
Answer 27
সাধারণত int।
Answer 28
হ্যাঁ। Operands যদি int হয়, result long variable-এ assign হওয়ার আগেই overflow হতে পারে।
Answer 29
Binary floating-point representation সব decimal value exactly store করতে পারে না।
Answer 30
Parentheses, intermediate variables এবং clear formatting ব্যবহার করা উচিত।
Lesson Summary
এই lesson-এ আমরা শিখেছি:
- Operator value-এর ওপর operation চালায়
- Operand হলো operator-এর input value
- Expression evaluate হয়ে result তৈরি করে
- Arithmetic operator calculation করে
+,-,*,/এবং%basic arithmetic operator- Integer division fractional অংশ বাদ দেয়
- Decimal result-এর জন্য floating-point operand প্রয়োজন
%remainder return করে=assignment এবং==equality comparison- Compound assignment existing value update করে
- Comparison operator boolean result দেয়
&&দুই condition true হলে true||অন্তত একটি condition true হলে true!boolean value reverse করে&&এবং||short-circuit evaluation ব্যবহার করে- Null check logical expression-এর শুরুতে রাখা গুরুত্বপূর্ণ
- Increment এবং decrement value
1করে পরিবর্তন করে - Prefix আগে update করে
- Postfix পরে update করে
- Complex increment expression avoid করা উচিত
- Operator precedence evaluation order নির্ধারণ করে
- Parentheses calculation এবং logic পরিষ্কার করে
- Multiplication এবং division addition-এর আগে evaluate হয়
+String concatenation-এও ব্যবহৃত হয়- String concatenation left-to-right execute হয়
- Ternary operator simple condition অনুযায়ী value select করে
- Floating-point equality-এর জন্য tolerance ব্যবহার করা যায়
- String content comparison-এর জন্য
.equals()ব্যবহার করা উচিত - Integer overflow expression evaluate হওয়ার সময় ঘটতে পারে
- Clear intermediate variables debugging এবং readability improve করে
Next Lesson
পরবর্তী lesson-এ আমরা Java type conversion এবং casting শিখব।
আমরা জানব:
- Type conversion কী
- Widening conversion
- Narrowing conversion
- Implicit conversion
- Explicit casting
- Numeric type conversion
- Data loss
- Overflow during casting
charএবং numeric conversion- String থেকে number conversion
- Number থেকে String conversion
- Invalid conversion
- Common casting-related error