Programming and Java Fundamentals
Variables and Constants
আপনি একটি free preview lesson দেখছেন।
Lesson Overview
Program চলার সময় বিভিন্ন ধরনের data সংরক্ষণ, ব্যবহার এবং পরিবর্তন করতে হয়।
উদাহরণ:
- একজন student-এর নাম
- একজন user-এর বয়স
- একটি product-এর price
- একটি course-এর duration
- একজন learner enrolled কি না
- একটি calculation-এর result
Java-তে এই data সংরক্ষণের জন্য variable ব্যবহার করা হয়।
Variable-এর value program চলার সময় পরিবর্তন করা যেতে পারে। অন্যদিকে, এমন কিছু value থাকে যেগুলো একবার নির্ধারণ করার পর পরিবর্তন করা উচিত নয়। এই ধরনের value-এর জন্য Java-তে final keyword ব্যবহার করা হয়।
এই lesson-এ আমরা শিখব:
- Variable কী
- Variable declaration
- Variable initialization
- Assignment এবং reassignment
- Variable type
- Variable naming
- Local variable
- Variable scope-এর প্রাথমিক ধারণা
finalkeyword- Constant
- Multiple variable declaration
- Type inference-এর জন্য
var - Common variable-related error
Learning Objectives
এই lesson শেষ করার পর আপনি পারবেন:
- Variable কী, তা ব্যাখ্যা করতে
- Java variable declare করতে
- Variable initialize করতে
- Variable-এর value assign এবং update করতে
- Declaration, initialization এবং assignment-এর পার্থক্য বুঝতে
- Meaningful variable name ব্যবহার করতে
- Local variable-এর basic scope বুঝতে
finalvariable তৈরি করতে- Constant naming convention follow করতে
- Multiple variable declaration-এর সমস্যা বুঝতে
varব্যবহারের basic rule ব্যাখ্যা করতে- Common variable-related compilation error শনাক্ত করতে
What Is a Variable?
Variable হলো program-এর মধ্যে একটি named storage location, যেখানে একটি value রাখা যায়।
উদাহরণ:
int age = 30;
এখানে:
intহলো data typeageহলো variable name30হলো stored value
Conceptually:
Variable name: age
Variable type: int
Variable value: 30
Variable ব্যবহার করে value পরে access করা যায়:
int age = 30;
System.out.println(age);
Output:
30
Why Do We Need Variables?
Variable ছাড়া একই value বারবার সরাসরি লিখতে হতো।
উদাহরণ:
System.out.println("Java and OOP Foundation");
System.out.println("Java and OOP Foundation");
System.out.println("Java and OOP Foundation");
Variable ব্যবহার করলে:
String courseName = "Java and OOP Foundation";
System.out.println(courseName);
System.out.println(courseName);
System.out.println(courseName);
এর সুবিধা:
- Value এক জায়গায় রাখা যায়
- Code readable হয়
- একই value reuse করা যায়
- Value পরিবর্তন করা সহজ হয়
- Calculation করা যায়
- Program state track করা যায়
Variable Declaration
Variable declaration-এর মাধ্যমে compiler-কে জানানো হয়:
- Variable-এর type কী
- Variable-এর নাম কী
Syntax:
dataType variableName;
Example:
int age;
এখানে:
Data type: int
Variable name: age
আরও example:
String studentName;
double coursePrice;
boolean isAvailable;
char grade;
এই variableগুলো declare করা হয়েছে, কিন্তু এখনো value assign করা হয়নি।
Variable Initialization
Variable declare করার সময় initial value দেওয়াকে initialization বলা হয়।
Syntax:
dataType variableName = value;
Example:
int age = 30;
আরও example:
String studentName = "Sakib";
double coursePrice = 4990.0;
boolean isAvailable = true;
char grade = 'A';
এখানে declaration এবং initialization একই statement-এ হয়েছে।
Declaration and Initialization Separately
Variable আগে declare করে পরে value assign করা যায়।
int age;
age = 30;
এখানে:
int age;
Variable declaration।
age = 30;
Assignment।
Complete example:
public class Main {
public static void main(String[] args) {
int age;
age = 30;
System.out.println(age);
}
}
Output:
30
Declaration, Initialization, and Assignment
এই তিনটি term আলাদা।
Declaration
Variable-এর type এবং name define করা:
int age;
Initialization
Variable-কে প্রথম value দেওয়া:
int age = 30;
অথবা:
int age;
age = 30;
Assignment
Variable-এর মধ্যে value রাখা:
age = 30;
Variable আগেই value পেয়ে থাকলে নতুন value assign করাও assignment।
age = 31;
Reassignment
Variable-এর value পরিবর্তন করা যায়।
Example:
int age = 30;
age = 31;
System.out.println(age);
Output:
31
প্রথমে:
age = 30
পরে:
age = 31
নতুন value পুরোনো value replace করেছে।
Reassignment Example
public class Main {
public static void main(String[] args) {
int enrolledStudentCount = 10;
System.out.println(enrolledStudentCount);
enrolledStudentCount = 15;
System.out.println(enrolledStudentCount);
}
}
Output:
10
15
Assignment Operator
Assignment operator:
=
এর কাজ ডান পাশের value evaluate করে বাম পাশের variable-এ রাখা।
int total = 10 + 20;
Conceptually:
10 + 20calculate হয়- Result
30 30valuetotalvariable-এ assign হয়
total = 30
Assignment Is Not Equality
Mathematics-এ:
x = 10
Equality বোঝাতে পারে।
Programming-এ:
x = 10;
মানে 10 value x variable-এ assign করা।
Comparison-এর জন্য Java-তে ব্যবহার করা হয়:
==
Example:
age == 18
Assignment:
age = 18;
Comparison:
age == 18
এগুলো আলাদা operation।
Reading a Variable
Variable-এর value ব্যবহার করাকে সাধারণভাবে variable read করা বলা যায়।
int age = 30;
System.out.println(age);
এখানে age variable-এর value read করে println method-এ দেওয়া হয়েছে।
আরেকটি example:
int firstNumber = 10;
int secondNumber = 20;
int total = firstNumber + secondNumber;
এখানে:
firstNumberread করা হয়েছেsecondNumberread করা হয়েছে- Result
total-এ assign করা হয়েছে
Variable Type
Java একটি statically typed language।
প্রতিটি variable-এর একটি নির্দিষ্ট type থাকে।
Example:
int age = 30;
age variable-এর type:
int
এটি integer value রাখবে।
নিচের assignment valid নয়:
int age = "thirty";
কারণ "thirty" একটি String, কিন্তু variable-এর type int।
Compiler error দিতে পারে:
incompatible types
Common Variable Types
Java-তে আমরা বিভিন্ন ধরনের value-এর জন্য বিভিন্ন type ব্যবহার করি।
int age = 30;
double price = 99.50;
boolean isAvailable = true;
char grade = 'A';
String courseName = "Java Foundation";
| Type | Example Value |
|---|---|
int | 30 |
double | 99.50 |
boolean | true |
char | 'A' |
String | "Java Foundation" |
Primitive data type পরবর্তী lesson-এ বিস্তারিত শেখানো হবে।
Variable Name
Variable name value-এর meaning প্রকাশ করা উচিত।
Poor:
int x = 30;
Better:
int studentAge = 30;
Poor:
double p = 4990.0;
Better:
double coursePrice = 4990.0;
Poor:
boolean b = true;
Better:
boolean isAvailable = true;
Variable Naming Rules
Variable name Java identifier rule follow করে।
Valid:
int age;
int studentAge;
int course2Price;
int _internalValue;
Invalid:
int 2ndAge;
int student age;
int course-price;
int class;
Variable Naming Convention
Java variable সাধারণত camelCase follow করে।
Good:
studentName
coursePrice
totalMarks
isAvailable
numberOfStudents
Avoid:
StudentName
course_price
TOTALMARKS
student-name
Boolean Variable Names
Boolean variable-এর নাম এমন হওয়া ভালো, যা true বা false question-এর মতো শোনায়।
Good:
boolean isActive;
boolean hasPermission;
boolean canEnroll;
boolean shouldRetry;
boolean paymentCompleted;
Less clear:
boolean status;
boolean flag;
boolean value;
Example:
boolean isCourseAvailable = true;
Code দেখে বোঝা যায়:
Course available কি না।
Using Variables in Output
public class Main {
public static void main(String[] args) {
String courseName = "Java and OOP Foundation";
String courseLanguage = "Bangla";
int durationInWeeks = 8;
boolean isBeginnerFriendly = true;
System.out.println("Course: " + courseName);
System.out.println("Language: " + courseLanguage);
System.out.println("Duration: " + durationInWeeks + " weeks");
System.out.println("Beginner friendly: " + isBeginnerFriendly);
}
}
Output:
Course: Java and OOP Foundation
Language: Bangla
Duration: 8 weeks
Beginner friendly: true
Using Variables in Calculations
int firstNumber = 10;
int secondNumber = 20;
int total = firstNumber + secondNumber;
System.out.println(total);
Output:
30
আরেকটি example:
int productPrice = 500;
int quantity = 3;
int totalPrice = productPrice * quantity;
System.out.println(totalPrice);
Output:
1500
Updating a Variable Using Its Current Value
Variable-এর current value ব্যবহার করে variable update করা যায়।
int score = 10;
score = score + 5;
System.out.println(score);
Output:
15
Execution:
- Current
scorevalue হলো10 10 + 5calculate হয়- Result
15 15আবারscore-এ assign হয়
Another Update Example
int enrolledStudentCount = 100;
enrolledStudentCount = enrolledStudentCount + 1;
System.out.println(enrolledStudentCount);
Output:
101
Compound Assignment Preview
নিচের code:
score = score + 5;
Short form-এ লেখা যায়:
score += 5;
আরও example:
score -= 2;
score *= 3;
score /= 2;
এগুলোকে compound assignment operator বলা হয়।
Operators lesson-এ এগুলো বিস্তারিত শেখানো হবে।
Declaring Multiple Variables
একই type-এর একাধিক variable একটি statement-এ declare করা যায়।
int firstNumber, secondNumber, total;
Initialization-সহ:
int firstNumber = 10, secondNumber = 20;
Technically valid হলেও একটি line-এ একাধিক variable declaration avoid করা ভালো।
Recommended:
int firstNumber = 10;
int secondNumber = 20;
এতে code:
- বেশি readable
- সহজে edit করা যায়
- সহজে comment করা যায়
- merge conflict কম হয়
- debugging সহজ হয়
Do Not Repeat the Type Incorrectly
Wrong:
int firstNumber = 10, int secondNumber = 20;
Correct option 1:
int firstNumber = 10, secondNumber = 20;
Recommended option:
int firstNumber = 10;
int secondNumber = 20;
Declaring Without Initialization
Local variable declare করে value না দিলে, use করার আগে initialize করতে হবে।
Wrong:
public class Main {
public static void main(String[] args) {
int age;
System.out.println(age);
}
}
Compiler error হতে পারে:
variable age might not have been initialized
Correct:
public class Main {
public static void main(String[] args) {
int age = 30;
System.out.println(age);
}
}
অথবা:
public class Main {
public static void main(String[] args) {
int age;
age = 30;
System.out.println(age);
}
}
Why Java Requires Local Variable Initialization
Java compiler uninitialized local variable read করতে দেয় না।
এতে unpredictable value ব্যবহার হওয়ার risk কমে।
Example:
int total;
System.out.println(total);
Compiler জানে না total-এর valid value কী।
এই কারণে use করার আগে value assign করতে হয়।
Local Variables
Method-এর body-এর মধ্যে declare করা variable-কে local variable বলা হয়।
Example:
public class Main {
public static void main(String[] args) {
int age = 30;
String name = "Sakib";
System.out.println(name);
System.out.println(age);
}
}
এখানে:
age
name
দুটি local variable।
Local Variable Lifetime
Local variable সাধারণত method বা block execute হওয়ার সময় exist করে।
Method শেষ হলে local variable-এর scope এবং execution context শেষ হয়।
Conceptually:
Enter method
↓
Create local variables
↓
Use local variables
↓
Exit method
↓
Local variables are no longer accessible
Variable Scope
Scope নির্ধারণ করে variable code-এর কোন অংশ থেকে accessible।
Example:
public class Main {
public static void main(String[] args) {
int age = 30;
System.out.println(age);
}
}
age variable main method-এর block-এর মধ্যে accessible।
Method-এর বাইরে এটি access করা যাবে না।
Wrong:
public class Main {
public static void main(String[] args) {
int age = 30;
}
static void displayAge() {
System.out.println(age);
}
}
displayAge method main method-এর local age variable দেখতে পারে না।
Block Scope
Curly braces একটি block তৈরি করে।
Block-এর মধ্যে declare করা variable সাধারণত সেই block-এর বাইরে accessible নয়।
Example:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
String message = "Adult";
System.out.println(message);
}
}
}
message শুধু if block-এর মধ্যে accessible।
Wrong:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
String message = "Adult";
}
System.out.println(message);
}
}
Compiler message variable খুঁজে পাবে না।
Inner Block Can Access Outer Variable
Inner block outer scope-এর variable access করতে পারে।
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println(age);
}
}
}
এখানে if block age access করতে পারে।
কারণ age outer block-এ declare করা হয়েছে।
Outer Block Cannot Access Inner Variable
public class Main {
public static void main(String[] args) {
if (true) {
int score = 100;
System.out.println(score);
}
// score is not accessible here
}
}
Inner block-এর variable outer block থেকে access করা যায় না।
Duplicate Local Variable Names
একই scope-এর মধ্যে একই নামের local variable দুবার declare করা যায় না।
Wrong:
int age = 30;
int age = 40;
Compiler error দিতে পারে:
variable age is already defined
Reassignment করতে হলে type আবার লিখবেন না।
Correct:
int age = 30;
age = 40;
Declaration Versus Reassignment
Wrong:
int score = 80;
int score = 90;
Correct:
int score = 80;
score = 90;
প্রথম statement declaration এবং initialization।
দ্বিতীয় statement reassignment।
Shadowing Preview
Different scope-এ একই নাম ব্যবহার করা কিছু context-এ possible হলেও confusing হতে পারে।
Class field এবং local variable-এর ক্ষেত্রে shadowing হতে পারে।
Example concept:
public class Student {
String name = "Class field";
void displayName() {
String name = "Local variable";
System.out.println(name);
}
}
এখানে local name field name-কে shadow করে।
এই concept classes and objects module-এ বিস্তারিত শেখানো হবে।
Beginner হিসেবে একই meaning-এর জন্য একই বা nested scope-এ repeated name avoid করা ভালো।
Changing a Variable's Type
একবার variable declare করলে তার type পরিবর্তন করা যায় না।
int age = 30;
এখন age সবসময় int type variable।
Invalid:
age = "thirty";
Type পরিবর্তন করতে নতুন variable প্রয়োজন:
int age = 30;
String ageInWords = "thirty";
Assigning Compatible Values
double price = 4990;
এখানে integer literal 4990 automatically double-এ represent হতে পারে।
কিন্তু:
int price = 4990.50;
এটি invalid।
কারণ decimal value সরাসরি int-এ রাখা যায় না।
Type conversion এবং casting future lesson-এ শেখানো হবে।
Copying Values Between Variables
এক variable-এর value অন্য variable-এ assign করা যায়।
int originalScore = 80;
int copiedScore = originalScore;
System.out.println(copiedScore);
Output:
80
Primitive value-এর ক্ষেত্রে value copy হয়।
originalScore = 90;
System.out.println(originalScore);
System.out.println(copiedScore);
Output:
90
80
copiedScore আগের copied value রাখে।
Reference type-এর behaviour কিছুটা আলাদা হতে পারে। Object lesson-এ এটি বিস্তারিত শেখানো হবে।
Swapping Variable Values
ধরা যাক:
int firstNumber = 10;
int secondNumber = 20;
Value swap করতে temporary variable ব্যবহার করা যায়।
int temporaryNumber = firstNumber;
firstNumber = secondNumber;
secondNumber = temporaryNumber;
Complete example:
public class Main {
public static void main(String[] args) {
int firstNumber = 10;
int secondNumber = 20;
int temporaryNumber = firstNumber;
firstNumber = secondNumber;
secondNumber = temporaryNumber;
System.out.println(firstNumber);
System.out.println(secondNumber);
}
}
Output:
20
10
What Is final?
final keyword ব্যবহার করলে variable-এ একবার value assign করার পর আবার নতুন value assign করা যায় না।
Example:
final int minimumAge = 18;
Reassignment invalid:
minimumAge = 21;
Compiler error হবে।
Final Variable
final int courseDurationInWeeks = 8;
এখানে:
- Variable-এর type
int - Variable name
courseDurationInWeeks - Initial value
8 finalহওয়ার কারণে reassignment করা যাবে না
Correct use:
System.out.println(courseDurationInWeeks);
Invalid:
courseDurationInWeeks = 10;
Final Variable Can Be Assigned Later
final variable declare করার সময় value না দিয়ে পরে একবার assign করা যায়।
final int minimumAge;
minimumAge = 18;
এরপর আর assign করা যাবে না।
Invalid:
minimumAge = 21;
একটি final local variable exactly একবার assign হতে পারে।
Constant
Program-এ এমন fixed value, যা পরিবর্তন করা উচিত নয়, তাকে সাধারণভাবে constant বলা হয়।
Java-তে class-level constant প্রায়ই এভাবে লেখা হয়:
static final int MINIMUM_VOTING_AGE = 18;
এখানে:
staticclass-এর সঙ্গে সম্পর্কিতfinalreassignment prevent করেinttypeMINIMUM_VOTING_AGEconstant name18value
Constant Naming Convention
Constant সাধারণত UPPER_SNAKE_CASE follow করে।
Good:
static final int MAXIMUM_LOGIN_ATTEMPTS = 5;
static final int MINIMUM_VOTING_AGE = 18;
static final double TAX_RATE = 0.15;
static final String DEFAULT_LANGUAGE = "Bangla";
Avoid:
static final int maximumLoginAttempts = 5;
static final int MaximumLoginAttempts = 5;
Technically compile করতে পারে, কিন্তু Java convention follow করে না।
Local Final Variable Versus Class Constant
Local final variable:
public static void main(String[] args) {
final int minimumAge = 18;
}
Class-level constant:
public class Main {
static final int MINIMUM_AGE = 18;
public static void main(String[] args) {
System.out.println(MINIMUM_AGE);
}
}
সব final variable necessarily shared constant নয়।
Constant বলতে সাধারণত meaningful fixed value বোঝানো হয়, যা class-level-এ reuse করা হয়।
Why Use Constants?
Constant ব্যবহার করলে:
- Fixed value accidentally change হয় না
- Magic number কমে
- Value-এর meaning পরিষ্কার হয়
- এক জায়গা থেকে update করা যায়
- Reuse সহজ হয়
- Code maintainable হয়
Without constant:
if (age >= 18) {
System.out.println("Eligible");
}
Better:
static final int MINIMUM_VOTING_AGE = 18;
Then:
if (age >= MINIMUM_VOTING_AGE) {
System.out.println("Eligible");
}
Magic Numbers
Code-এর মধ্যে explanation ছাড়া literal number ব্যবহার করলে সেটিকে magic number বলা হয়।
Example:
if (loginAttempts >= 5) {
System.out.println("Account locked");
}
5 কেন ব্যবহার করা হয়েছে, name থেকে বোঝা যাচ্ছে না।
Better:
static final int MAXIMUM_LOGIN_ATTEMPTS = 5;
Then:
if (loginAttempts >= MAXIMUM_LOGIN_ATTEMPTS) {
System.out.println("Account locked");
}
Do Not Make Everything a Constant
সব value constant হওয়া উচিত নয়।
Variable:
int currentLoginAttempts = 0;
এটি change করবে।
Constant:
static final int MAXIMUM_LOGIN_ATTEMPTS = 5;
এটি fixed rule।
Difference:
currentLoginAttempts → state, changes
MAXIMUM_LOGIN_ATTEMPTS → rule, fixed
Mutable State and Fixed Rules
Example:
public class Main {
static final int MAXIMUM_CAPACITY = 100;
public static void main(String[] args) {
int currentStudentCount = 80;
currentStudentCount = 85;
System.out.println("Current students: " + currentStudentCount);
System.out.println("Maximum capacity: " + MAXIMUM_CAPACITY);
}
}
Output:
Current students: 85
Maximum capacity: 100
Using var
Modern Java-তে local variable declare করার জন্য var ব্যবহার করা যায়।
Example:
var age = 30;
var courseName = "Java Foundation";
var isAvailable = true;
Compiler assigned value দেখে type infer করে।
Conceptually:
var age = 30;
Compiler এটিকে int type হিসেবে বুঝতে পারে।
var courseName = "Java Foundation";
Compiler এটিকে String type হিসেবে বুঝতে পারে।
var Is Not Dynamic Typing
var ব্যবহার করলেও Java statically typed থাকে।
var age = 30;
Compiler age-এর type int নির্ধারণ করে।
পরে invalid:
age = "thirty";
Type পরিবর্তন করা যায় না।
var Requires Initialization
Invalid:
var age;
Compiler type infer করতে পারবে না।
Correct:
var age = 30;
Value দেখে type নির্ধারণ করা হয়।
var Is for Local Variables
var সাধারণত local variable-এর জন্য ব্যবহার করা হয়।
Example:
public static void main(String[] args) {
var courseName = "Java Foundation";
}
Field declaration-এ সাধারণভাবে ব্যবহার করা যায় না।
Invalid concept:
public class Course {
var courseName = "Java Foundation";
}
Should Beginners Use var?
শুরুতে explicit type ব্যবহার করলে data type ভালোভাবে বোঝা যায়।
Recommended for learning:
int age = 30;
String courseName = "Java Foundation";
boolean isAvailable = true;
var useful যখন type assigned value থেকে খুব clear।
Example:
var studentCount = 100;
কিন্তু এমন code avoid করা উচিত যেখানে inferred type clear নয়।
Strong foundation তৈরি না হওয়া পর্যন্ত explicit type বেশি useful।
Variable Declaration Placement
Variable প্রয়োজনের কাছাকাছি declare করা ভালো।
Poor:
int totalPrice;
// Many unrelated statements
totalPrice = productPrice * quantity;
Better:
int totalPrice = productPrice * quantity;
এর ফলে:
- Scope ছোট থাকে
- Code বুঝতে সহজ হয়
- Uninitialized use কমে
- Related logic একসঙ্গে থাকে
Minimize Variable Scope
যে block-এ variable প্রয়োজন, সেই block-এর মধ্যে declare করুন।
Poor:
public static void main(String[] args) {
String eligibilityMessage;
int age = 20;
if (age >= 18) {
eligibilityMessage = "Eligible";
System.out.println(eligibilityMessage);
}
}
Better:
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
String eligibilityMessage = "Eligible";
System.out.println(eligibilityMessage);
}
}
যদি variable শুধু if block-এর মধ্যে প্রয়োজন হয়, scope ছোট রাখুন।
One Variable, One Meaning
একটি variable-এর meaning মাঝপথে পরিবর্তন করবেন না।
Poor:
int value = 18;
// Later
value = 4990;
// Later
value = 8;
একই variable কখনো age, কখনো price, কখনো duration represent করছে।
Better:
int minimumAge = 18;
int coursePrice = 4990;
int durationInWeeks = 8;
Avoid Reusing Variables Unnecessarily
Poor:
int result = 10 + 20;
System.out.println(result);
result = 5 * 7;
System.out.println(result);
Code valid, কিন্তু calculation-এর meaning unclear।
Better:
int additionResult = 10 + 20;
int multiplicationResult = 5 * 7;
System.out.println(additionResult);
System.out.println(multiplicationResult);
Variable Value Tracing
Code:
int score = 10;
score = score + 5;
score = score * 2;
score = score - 4;
Step-by-step:
Initial score = 10
score = 10 + 5 = 15
score = 15 × 2 = 30
score = 30 - 4 = 26
Final value:
26
Example: Student Marks
public class Main {
public static void main(String[] args) {
int mathematicsMark = 80;
int englishMark = 90;
int scienceMark = 85;
int totalMarks =
mathematicsMark
+ englishMark
+ scienceMark;
System.out.println("Mathematics: " + mathematicsMark);
System.out.println("English: " + englishMark);
System.out.println("Science: " + scienceMark);
System.out.println("Total: " + totalMarks);
}
}
Output:
Mathematics: 80
English: 90
Science: 85
Total: 255
Example: Course Enrollment
public class Main {
static final int MAXIMUM_STUDENT_CAPACITY = 100;
public static void main(String[] args) {
String courseName = "Java and OOP Foundation";
int enrolledStudentCount = 75;
boolean enrollmentOpen = true;
enrolledStudentCount = enrolledStudentCount + 1;
System.out.println("Course: " + courseName);
System.out.println("Enrolled students: " + enrolledStudentCount);
System.out.println("Maximum capacity: " + MAXIMUM_STUDENT_CAPACITY);
System.out.println("Enrollment open: " + enrollmentOpen);
}
}
Output:
Course: Java and OOP Foundation
Enrolled students: 76
Maximum capacity: 100
Enrollment open: true
Example: Product Order
public class Main {
static final int DELIVERY_CHARGE = 80;
public static void main(String[] args) {
String productName = "Java Book";
int productPrice = 500;
int quantity = 3;
int subtotal = productPrice * quantity;
int totalPrice = subtotal + DELIVERY_CHARGE;
System.out.println("Product: " + productName);
System.out.println("Price: " + productPrice);
System.out.println("Quantity: " + quantity);
System.out.println("Subtotal: " + subtotal);
System.out.println("Delivery charge: " + DELIVERY_CHARGE);
System.out.println("Total: " + totalPrice);
}
}
Output:
Product: Java Book
Price: 500
Quantity: 3
Subtotal: 1500
Delivery charge: 80
Total: 1580
Common Error: Using an Undeclared Variable
Wrong:
System.out.println(courseName);
যদি courseName আগে declare না করা হয়, compiler error হবে।
Correct:
String courseName = "Java Foundation";
System.out.println(courseName);
Common Error: Using an Uninitialized Local Variable
Wrong:
int age;
System.out.println(age);
Correct:
int age = 30;
System.out.println(age);
Common Error: Type Mismatch
Wrong:
int age = "30";
"30" একটি String।
Correct:
int age = 30;
অথবা text প্রয়োজন হলে:
String age = "30";
Common Error: Redeclaring the Same Variable
Wrong:
int age = 30;
int age = 31;
Correct:
int age = 30;
age = 31;
Common Error: Assigning to a Final Variable
Wrong:
final int minimumAge = 18;
minimumAge = 21;
minimumAge reassignment করা যাবে না।
Correct:
final int minimumAge = 18;
Common Error: Using var Without a Value
Wrong:
var courseName;
Correct:
var courseName = "Java Foundation";
Common Error: Invalid Variable Name
Wrong:
int student age = 20;
Correct:
int studentAge = 20;
Wrong:
int 2ndMark = 90;
Correct:
int secondMark = 90;
Common Error: Confusing Declaration with Assignment
Wrong:
int age = 30;
int age = 31;
Correct:
int age = 30;
age = 31;
Variable already declared হলে type আবার লিখতে হয় না।
Common Error: Incorrect Constant Naming
Technically valid:
static final int maximumCapacity = 100;
Recommended:
static final int MAXIMUM_CAPACITY = 100;
Common Error: Assigning the Wrong Variable
int mathematicsMark = 80;
int englishMark = 90;
mathematicsMark = 95;
যদি intention ছিল English mark update করা, তাহলে wrong variable update হয়েছে।
Correct:
englishMark = 95;
Meaningful naming এই ধরনের error কমাতে সাহায্য করে।
Debugging Variable Values
Program expected result না দিলে variable print করে value inspect করা যায়।
int price = 500;
int quantity = 3;
int total = price + quantity;
System.out.println("price = " + price);
System.out.println("quantity = " + quantity);
System.out.println("total = " + total);
Output:
price = 500
quantity = 3
total = 503
এখানে দেখা যাচ্ছে calculation-এ + ব্যবহার করা হয়েছে, কিন্তু expected ছিল multiplication।
Correct:
int total = price * quantity;
Debugging-এর সময় variable value দেখা useful।
Variable Checklist
Variable তৈরি করার আগে নিজেকে প্রশ্ন করুন:
- Valueটির meaning কী?
- সঠিক data type কী?
- Variable-এর meaningful name কী?
- Value পরিবর্তন হবে কি?
- Fixed হলে
finalব্যবহার করা উচিত কি? - Scope কতটুকু প্রয়োজন?
- Initial value এখনই জানা আছে কি?
- একই value constant হিসেবে reuse হবে কি?
Important Terms
Variable
একটি named storage location, যেখানে value রাখা যায়।
Data Type
Variable কোন ধরনের value রাখতে পারবে, তা নির্ধারণ করে।
Declaration
Variable-এর type এবং name define করা।
int age;
Initialization
Variable-কে প্রথম value দেওয়া।
int age = 30;
Assignment
Variable-এর মধ্যে value রাখা।
age = 30;
Reassignment
আগের value replace করে নতুন value assign করা।
age = 31;
Assignment Operator
ডান পাশের value বাম পাশের variable-এ রাখে।
=
Local Variable
Method বা block-এর মধ্যে declare করা variable।
Scope
Variable code-এর কোন অংশ থেকে accessible, সেই সীমা।
final
Variable-কে একবার assign করার পর reassignment prevent করে।
Constant
Program-এর fixed এবং meaningful value।
Magic Number
Code-এর মধ্যে explanation ছাড়া ব্যবহৃত literal number।
var
Local variable-এর type compiler-কে assigned value থেকে infer করতে দেয়।
Type Inference
Compiler value দেখে variable-এর type নির্ধারণ করে।
Practice Exercise 1: Identify the Parts
নিচের statement দেখুন:
int studentAge = 20;
Identify করুন:
- Data type
- Variable name
- Assignment operator
- Initial value
- Statement terminator
Practice Exercise 2: Declaration or Initialization?
প্রতিটি statement declaration, initialization নাকি assignment, তা লিখুন।
Example 1
int age;
Example 2
int age = 30;
Example 3
age = 31;
Example 4
String courseName;
Example 5
courseName = "Java Foundation";
Practice Exercise 3: Create Variables
নিচের information-এর জন্য appropriate variable তৈরি করুন:
- Student name
- Student age
- Course price
- Course duration in weeks
- Course available কি না
- Student grade
- Total number of lessons
Meaningful name এবং appropriate type ব্যবহার করুন।
Practice Exercise 4: Predict the Output
public class Main {
public static void main(String[] args) {
int score = 10;
score = score + 5;
score = score * 2;
System.out.println(score);
}
}
Program run করার আগে final output লিখুন।
Practice Exercise 5: Trace the Variable
নিচের code step-by-step trace করুন:
int balance = 1000;
balance = balance + 500;
balance = balance - 200;
balance = balance * 2;
প্রতিটি statement-এর পরে balance-এর value লিখুন।
Practice Exercise 6: Fix the Errors
নিচের code fix করুন:
public class Main {
public static void main(String[] args) {
int student age;
student age = "20";
System.out.println(student age);
}
}
Expected output:
20
Practice Exercise 7: Fix the Redeclaration
int coursePrice = 4990;
int coursePrice = 3990;
System.out.println(coursePrice);
Codeটি fix করুন, যাতে output হয়:
3990
Practice Exercise 8: Create Constants
নিচের fixed value-এর জন্য constant তৈরি করুন:
- Minimum voting age:
18 - Maximum login attempts:
5 - Course duration in weeks:
8 - Default language:
"Bangla" - Tax rate:
0.15
static final এবং UPPER_SNAKE_CASE ব্যবহার করুন।
Practice Exercise 9: Replace Magic Numbers
Before:
int loginAttempts = 5;
if (loginAttempts >= 5) {
System.out.println("Account locked");
}
5 value-এর জন্য meaningful constant তৈরি করুন এবং code rewrite করুন।
Practice Exercise 10: Local Scope
নিচের code compile করবে কি না ব্যাখ্যা করুন:
public class Main {
public static void main(String[] args) {
if (true) {
String message = "Hello";
}
System.out.println(message);
}
}
Compile না করলে reason এবং correct version লিখুন।
Practice Exercise 11: Inner Scope
নিচের output predict করুন:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println(age);
}
}
}
Inner block কেন age access করতে পারে, তা ব্যাখ্যা করুন।
Practice Exercise 12: Use var
নিচের explicit variableগুলো var ব্যবহার করে লিখুন:
int age = 30;
String courseName = "Java Foundation";
boolean isAvailable = true;
double coursePrice = 4990.0;
তারপর প্রতিটি inferred type লিখুন।
Practice Exercise 13: Explain Why This Fails
var studentName;
এই code কেন compile করবে না?
Practice Exercise 14: Swap Values
দুটি variable:
int firstNumber = 10;
int secondNumber = 20;
Temporary variable ব্যবহার করে value swap করুন।
Expected final values:
firstNumber = 20
secondNumber = 10
Practice Exercise 15: Student Information Program
একটি program লিখুন, যেখানে থাকবে:
- Student name
- Student age
- Mathematics mark
- English mark
- Science mark
- Total mark
Expected output format:
Student: Sakib
Age: 20
Mathematics: 80
English: 90
Science: 85
Total: 255
নিজের value ব্যবহার করতে পারেন।
Practice Exercise 16: Course Information Program
নিচের requirement অনুযায়ী program লিখুন:
Constants:
COURSE_DURATION_IN_WEEKS = 8
MAXIMUM_STUDENT_CAPACITY = 100
Variables:
courseName
courseLanguage
enrolledStudentCount
isEnrollmentOpen
enrolledStudentCount একবার update করুন।
Expected output format:
Course: Java and OOP Foundation
Language: Bangla
Duration: 8 weeks
Enrolled students: 76
Maximum capacity: 100
Enrollment open: true
Practice Exercise 17: Product Order Program
Variables তৈরি করুন:
productName
productPrice
quantity
subtotal
deliveryCharge
totalPrice
Delivery charge constant করুন।
Formula:
subtotal = productPrice × quantity
totalPrice = subtotal + deliveryCharge
Complete output print করুন।
Practice Exercise 18: Identify Variable Quality
প্রতিটি variable name good নাকি poor, তা লিখুন এবং poor হলে improve করুন।
xstudentAgecourse_priceisAvailableflagtotalMarksStudentNamenumberOfLessonsppaymentCompleted
Practice Exercise 19: Final Variable
নিচের code-এ কী error হবে, তা লিখুন:
final int courseDuration = 8;
courseDuration = 10;
একটি correct version লিখুন।
Practice Exercise 20: Explain in Your Own Words
নিজের ভাষায় উত্তর দিন:
- Variable কী?
- Declaration এবং initialization-এর পার্থক্য কী?
- Assignment এবং reassignment কী?
- Variable-এর type কেন গুরুত্বপূর্ণ?
- Local variable use করার আগে initialize করতে হয় কেন?
- Scope কী?
finalকী করে?- Constant এবং normal variable-এর পার্থক্য কী?
- Magic number কী?
varকি Java-কে dynamically typed করে?
Knowledge Check
Question 1
Variable কী?
Question 2
Variable declaration-এর syntax কী?
Question 3
Initialization কী?
Question 4
Assignment operator কোনটি?
Question 5
Reassignment কী?
Question 6
Java-তে variable-এর type কি পরে পরিবর্তন করা যায়?
Question 7
Local variable কী?
Question 8
Uninitialized local variable read করা যায় কি?
Question 9
Variable scope কী?
Question 10
Inner block কি outer block-এর variable access করতে পারে?
Question 11
Outer block কি inner block-এর variable access করতে পারে?
Question 12
একই scope-এ একই নামের variable দুবার declare করা যায় কি?
Question 13
final keyword কী করে?
Question 14
Constant naming convention কী?
Question 15
Magic number কী?
Question 16
var কী করে?
Question 17
var variable কি type পরিবর্তন করতে পারে?
Question 18
var declare করার সময় initial value কেন প্রয়োজন?
Question 19
Multiple variable এক line-এ declare করা recommended কি?
Question 20
Meaningful variable name কেন গুরুত্বপূর্ণ?
Knowledge Check Answers
Answer 1
Variable হলো program-এর মধ্যে একটি named storage location, যেখানে value রাখা যায়।
Answer 2
dataType variableName;
Example:
int age;
Answer 3
Variable-কে প্রথম value দেওয়াকে initialization বলা হয়।
int age = 30;
Answer 4
Assignment operator:
=
Answer 5
Variable-এর existing value replace করে নতুন value assign করাকে reassignment বলা হয়।
age = 31;
Answer 6
না। Variable declare হওয়ার পর তার type পরিবর্তন করা যায় না।
Answer 7
Method বা block-এর মধ্যে declare করা variable হলো local variable।
Answer 8
না। Local variable use করার আগে initialize করতে হয়।
Answer 9
Scope নির্ধারণ করে variable code-এর কোন অংশ থেকে accessible।
Answer 10
হ্যাঁ। Inner block সাধারণত outer block-এর accessible variable ব্যবহার করতে পারে।
Answer 11
না। Inner block-এর local variable outer block থেকে accessible নয়।
Answer 12
না। একই scope-এ একই নামের local variable দুবার declare করা যায় না।
Answer 13
final variable-এ একবার value assign করার পর reassignment prevent করে।
Answer 14
Constant সাধারণত:
UPPER_SNAKE_CASE
follow করে।
Example:
MAXIMUM_LOGIN_ATTEMPTS
Answer 15
Meaningful name ছাড়া code-এর মধ্যে ব্যবহৃত unexplained literal number-কে magic number বলা হয়।
Answer 16
var assigned value দেখে compiler-কে local variable-এর type infer করতে দেয়।
Answer 17
না। Type infer হওয়ার পর variable statically typed থাকে।
Answer 18
Initial value ছাড়া compiler type infer করতে পারে না।
Answer 19
Technically valid হলেও readability-এর জন্য সাধারণত এক line-এ একটি variable declaration recommended।
Answer 20
Meaningful name variable-এর purpose এবং stored value-এর meaning বুঝতে সাহায্য করে।
Lesson Summary
এই lesson-এ আমরা শিখেছি:
- Variable program-এর data store করে
- Variable declaration type এবং name define করে
- Initialization variable-কে প্রথম value দেয়
- Assignment operator
=value variable-এ রাখে - Reassignment existing value replace করে
- Java variable statically typed
- Variable-এর type পরে পরিবর্তন করা যায় না
- Meaningful variable name code readable করে
- Java variable সাধারণত camelCase follow করে
- Boolean variable-এর নাম question-like হওয়া ভালো
- Variable calculation এবং output-এ reuse করা যায়
- Local variable method বা block-এর মধ্যে declare করা হয়
- Local variable use করার আগে initialize করতে হয়
- Scope variable কোথা থেকে accessible তা নির্ধারণ করে
- Inner block outer variable access করতে পারে
- Outer block inner variable access করতে পারে না
- একই scope-এ duplicate local variable declare করা যায় না
finalreassignment prevent করে- Constant fixed rule বা value represent করে
- Constant সাধারণত
UPPER_SNAKE_CASEfollow করে - Constant magic number কমায়
varlocal variable-এর type infer করতে দেয়varJava-কে dynamically typed করে না- Variable প্রয়োজনের কাছাকাছি declare করা ভালো
- Variable scope যত ছোট রাখা যায়, তত ভালো
- একটি variable-এর একটি পরিষ্কার meaning থাকা উচিত
Next Lesson
পরবর্তী lesson-এ আমরা Java primitive data types বিস্তারিতভাবে শিখব।
আমরা জানব:
- Primitive data type কী
byteshortintlongfloatdoublecharboolean- Integer এবং decimal literal
- Numeric range
- Overflow
- সঠিক data type নির্বাচন
- Primitive type-এর default value
- Common data type error