Programming and Java Fundamentals
Primitive Data Types
You are viewing a free preview lesson.
Lesson Overview
Java-তে প্রতিটি variable একটি নির্দিষ্ট ধরনের value সংরক্ষণ করে।
উদাহরণ:
- পূর্ণসংখ্যা
- দশমিক সংখ্যা
- একটি character
trueবাfalsevalue
এই basic valueগুলো সংরক্ষণের জন্য Java আটটি primitive data type দেয়।
Primitive data type হলো Java language-এর built-in fundamental type। এগুলো object নয় এবং সরাসরি একটি simple value represent করে।
Java-এর আটটি primitive data type হলো:
byteshortintlongfloatdoublecharboolean
এই lesson-এ আমরা প্রতিটি primitive type, তাদের range, literal syntax, memory usage এবং practical use case শিখব।
Learning Objectives
এই lesson শেষ করার পর আপনি পারবেন:
- Primitive data type কী, তা ব্যাখ্যা করতে
- Java-এর আটটি primitive type-এর নাম বলতে
- পূর্ণসংখ্যার জন্য সঠিক integer type নির্বাচন করতে
- Decimal value-এর জন্য
floatএবংdoubleব্যবহার করতে charএবংbooleantype ব্যবহার করতে- Numeric literal-এর suffix বুঝতে
- Underscore ব্যবহার করে বড় number readableভাবে লিখতে
- Integer overflow এবং underflow বুঝতে
- Floating-point precision limitation ব্যাখ্যা করতে
- Primitive এবং reference type-এর basic পার্থক্য বুঝতে
- Common type-related error identify করতে
What Is a Data Type?
Data type নির্ধারণ করে:
- Variable কী ধরনের value রাখতে পারবে
- সেই value memory-তে কীভাবে represent হবে
- Value-এর ওপর কোন operation করা যাবে
- Value-এর possible range কত
- Compiler কোন assignment accept করবে
Example:
int age = 30;
এখানে int জানায় যে age একটি integer value রাখবে।
double price = 4990.50;
এখানে double একটি decimal value রাখবে।
boolean isAvailable = true;
এখানে boolean শুধু true বা false value রাখবে।
What Is a Primitive Data Type?
Primitive data type হলো Java language-এর built-in basic data type।
Primitive variable সাধারণত সরাসরি একটি simple value represent করে।
Example:
int age = 30;
char grade = 'A';
boolean isPassed = true;
Java-এর primitive types:
byte
short
int
long
float
double
char
boolean
এগুলোকে broadly চারটি category-তে ভাগ করা যায়:
- Integer types
- Floating-point types
- Character type
- Boolean type
Primitive Type Categories
| Category | Types |
|---|---|
| Integer | byte, short, int, long |
| Floating-point | float, double |
| Character | char |
| Boolean | boolean |
Primitive Types at a Glance
| Type | Size | Example | General Purpose |
|---|---|---|---|
byte | 8 bits | 100 | খুব ছোট integer |
short | 16 bits | 30000 | ছোট integer |
int | 32 bits | 100000 | সাধারণ integer |
long | 64 bits | 9000000000L | বড় integer |
float | 32 bits | 10.5F | কম precision decimal |
double | 64 bits | 10.5 | সাধারণ decimal |
char | 16 bits | 'A' | একটি character |
boolean | JVM-dependent representation | true | Logical state |
Bits and Bytes
Computer data binary form-এ represent করে।
Binary digit:
0
1
একটি binary digit-কে bit বলা হয়।
আটটি bit মিলে সাধারণত একটি byte:
1 byte = 8 bits
Example:
byte = 8 bits
short = 16 bits
int = 32 bits
long = 64 bits
Type যত বেশি bit ব্যবহার করে, সাধারণত তার representable value range তত বড় হয়।
Integer Data Types
Integer type পূর্ণসংখ্যা সংরক্ষণ করে।
উদাহরণ:
-100
0
25
4990
1000000
Java-এর integer primitive types:
byteshortintlong
The byte Type
byte একটি 8-bit signed integer type।
Range:
-128 to 127
Example:
byte age = 25;
byte temperature = -10;
byte percentage = 100;
Valid byte Values
byte minimum = -128;
byte maximum = 127;
byte zero = 0;
Invalid:
byte value = 128;
কারণ 128 byte range-এর বাইরে।
Compiler error হতে পারে:
possible lossy conversion from int to byte
When to Use byte
byte ব্যবহার করা যেতে পারে যখন:
- Value নিশ্চিতভাবে
-128থেকে127range-এর মধ্যে - Large array-তে memory saving গুরুত্বপূর্ণ
- Binary file বা network data process করা হচ্ছে
- Low-level protocol value represent করা হচ্ছে
সাধারণ business application-এর normal integer-এর জন্য int বেশি common।
The short Type
short একটি 16-bit signed integer type।
Range:
-32,768 to 32,767
Example:
short roomNumber = 1200;
short temperature = -500;
short maximumScore = 30000;
Invalid:
short population = 100000;
কারণ value short range-এর বাইরে।
When to Use short
short খুব বেশি ব্যবহার করা হয় না।
Possible use case:
- Memory-sensitive large array
- External binary format
- Legacy data structure
- Value range clearly limited
সাধারণ calculation-এর জন্য int ব্যবহার করা বেশি straightforward।
The int Type
int একটি 32-bit signed integer type।
Range:
-2,147,483,648
to
2,147,483,647
Java-তে integer value-এর জন্য int সবচেয়ে commonly used type।
Example:
int age = 30;
int studentCount = 500;
int coursePrice = 4990;
int population = 1_000_000;
Integer Literal Defaults to int
নিচের literal:
100
সাধারণভাবে compiler এটিকে int হিসেবে বিবেচনা করে।
Example:
int score = 100;
Small value compatible হলে byte বা short-এও assign করা যায়:
byte percentage = 100;
short roomNumber = 1200;
Compiler compile-time constant দেখে range check করতে পারে।
When to Use int
সাধারণ পূর্ণসংখ্যার জন্য প্রথম choice হিসেবে int ব্যবহার করুন।
Example:
- Age
- Quantity
- Student count
- Marks
- Retry count
- Page number
- Duration in days
- Inventory count
- Order item count
int quantity = 5;
int totalMarks = 255;
int loginAttempts = 3;
The long Type
long একটি 64-bit signed integer type।
Range:
-9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
int range-এর চেয়ে বড় পূর্ণসংখ্যার জন্য long ব্যবহার করা হয়।
Example:
long worldPopulation = 8_000_000_000L;
long fileSizeInBytes = 5_000_000_000L;
long transactionId = 9_223_000_000L;
The L Suffix
Large integer literal-এর শেষে L যোগ করতে হয়।
Correct:
long worldPopulation = 8_000_000_000L;
Without suffix:
long worldPopulation = 8_000_000_000;
Compiler literalটিকে প্রথমে int হিসেবে interpret করার চেষ্টা করতে পারে এবং error দেবে, কারণ value int range-এর বাইরে।
Uppercase L Is Recommended
Technically lowercase l ব্যবহার করা যায়:
long value = 100l;
কিন্তু lowercase l দেখতে digit 1-এর মতো হতে পারে।
Recommended:
long value = 100L;
When to Use long
long ব্যবহার করা যায়:
- বড় count
- Timestamp
- Millisecond duration
- File size
- Large identifier
- Database
BIGINT - Monetary amount in minor units, যখন range বড় হতে পারে
Example:
long currentTimestamp = System.currentTimeMillis();
long accountBalanceInCents = 250_000L;
Comparing Integer Types
| Type | Bits | Minimum | Maximum |
|---|---|---|---|
byte | 8 | -128 | 127 |
short | 16 | -32,768 | 32,767 |
int | 32 | -2,147,483,648 | 2,147,483,647 |
long | 64 | প্রায় -9.22 quintillion | প্রায় 9.22 quintillion |
Choosing an Integer Type
সাধারণ rule:
- Defaultভাবে
int intrange যথেষ্ট না হলেlongbyteবাshortতখনই, যখন specific reason আছে
Example:
int studentAge = 20;
int courseLessonCount = 120;
long totalPlatformUsers = 5_000_000_000L;
Integer Literals
Integer literal বিভিন্ন number system-এ লেখা যায়।
Decimal Literal
সাধারণ base-10 number:
int decimalValue = 100;
Binary Literal
Binary prefix:
0b
Example:
int binaryValue = 0b1010;
0b1010 decimal 10 represent করে।
System.out.println(binaryValue);
Output:
10
Octal Literal
Octal literal 0 দিয়ে শুরু হয়।
int octalValue = 012;
এটি decimal 10 represent করে।
Octal literal beginner code-এ avoid করা ভালো, কারণ leading zero confusion তৈরি করতে পারে।
Hexadecimal Literal
Hexadecimal prefix:
0x
Example:
int hexadecimalValue = 0xA;
Output:
10
Hexadecimal commonly ব্যবহার করা হয়:
- Color
- Bit mask
- Memory-related representation
- Protocol
- Unicode value
Underscores in Numeric Literals
বড় number readable করার জন্য underscore ব্যবহার করা যায়।
int oneMillion = 1_000_000;
long worldPopulation = 8_000_000_000L;
double largeAmount = 1_500_000.50;
Compiler underscore ignore করে।
int value = 1_000;
এবং:
int value = 1000;
একই numeric value।
Invalid Underscore Placement
Invalid:
int value = _1000;
Invalid:
int value = 1000_;
Invalid:
double price = 100_.50;
Invalid:
double price = 100._50;
Valid:
double price = 100.50;
double price = 1_000.50;
Integer Arithmetic
int firstNumber = 10;
int secondNumber = 20;
int total = firstNumber + secondNumber;
int difference = secondNumber - firstNumber;
int product = firstNumber * secondNumber;
int quotient = secondNumber / firstNumber;
Output:
System.out.println(total); // 30
System.out.println(difference); // 10
System.out.println(product); // 200
System.out.println(quotient); // 2
Integer Division
Integer type দিয়ে division করলে decimal অংশ বাদ যায়।
int result = 5 / 2;
System.out.println(result);
Output:
2
Mathematical result 2.5, কিন্তু দুই operand-ই integer হওয়ায় result integer।
Decimal result পেতে:
double result = 5.0 / 2.0;
Output:
2.5
অথবা:
double result = 5 / 2.0;
Integer Overflow
Variable-এর maximum range-এর চেয়ে বড় result হলে overflow ঘটতে পারে।
Example:
int maximumValue = 2_147_483_647;
maximumValue = maximumValue + 1;
System.out.println(maximumValue);
Output:
-2147483648
Program crash না করে value wrap around করেছে।
এটিকে integer overflow বলা হয়।
Integer Underflow
Minimum value-এর নিচে গেলে underflow হতে পারে।
int minimumValue = -2_147_483_648;
minimumValue = minimumValue - 1;
System.out.println(minimumValue);
Output:
2147483647
Overflow Can Cause Serious Bugs
Overflow-এর কারণে হতে পারে:
- Wrong financial calculation
- Incorrect counter
- Security issue
- Negative quantity
- Invalid timestamp
- Wrong array size
- Incorrect limit checking
Range সম্পর্কে ধারণা রাখা গুরুত্বপূর্ণ।
Detecting Overflow
Java-এর Math class exact arithmetic method দেয়।
Example:
int result = Math.addExact(2_147_483_647, 1);
এটি silent overflow না করে exception throw করবে।
আরও method:
Math.subtractExact()
Math.multiplyExact()
Math.incrementExact()
Math.decrementExact()
এগুলো advanced usage হলেও overflow-sensitive calculation-এ useful।
Floating-Point Data Types
Decimal বা fractional value-এর জন্য floating-point type ব্যবহার করা হয়।
Java-এর floating-point primitive types:
floatdouble
Example value:
10.5
99.99
-20.75
3.14159
The float Type
float একটি 32-bit floating-point type।
এটি approximately 6–7 decimal digits precision দেয়।
Example:
float temperature = 36.5F;
float interestRate = 5.5F;
The F Suffix
Java-তে decimal literal defaultভাবে double।
এই কারণে float variable-এ value assign করতে suffix প্রয়োজন:
float price = 99.50F;
Without suffix:
float price = 99.50;
Compiler error:
possible lossy conversion from double to float
Uppercase F Recommended
Valid:
float value = 10.5f;
Recommended:
float value = 10.5F;
The double Type
double একটি 64-bit floating-point type।
এটি approximately 15–16 decimal digits precision দেয়।
Java-তে decimal calculation-এর default floating-point type হলো double।
Example:
double coursePrice = 4990.50;
double temperature = 36.75;
double average = 81.67;
Decimal Literal Defaults to double
double value = 10.5;
এখানে suffix প্রয়োজন নেই।
Optional D suffix ব্যবহার করা যায়:
double value = 10.5D;
তবে সাধারণত D লেখা হয় না।
Comparing float and double
| Type | Bits | Approximate Precision |
|---|---|---|
float | 32 | 6–7 decimal digits |
double | 64 | 15–16 decimal digits |
সাধারণ decimal calculation-এর জন্য double ব্যবহার করা বেশি common।
When to Use float
float ব্যবহার করা হতে পারে:
- Memory-sensitive large numeric array
- Graphics
- Game development
- Scientific system যেখানে 32-bit precision যথেষ্ট
- API বা file format specifically
floatrequire করে
সাধারণ business calculation-এর জন্য double বেশি convenient।
When to Use double
double ব্যবহার করা যায়:
- Average
- Percentage
- Measurement
- Temperature
- Distance
- General decimal calculation
- Scientific approximation
Example:
double averageMark = 81.67;
double completionPercentage = 75.5;
Floating-Point Precision
float এবং double binary floating-point representation ব্যবহার করে।
সব decimal value binary-তে exactly represent করা যায় না।
Example:
double result = 0.1 + 0.2;
System.out.println(result);
Possible output:
0.30000000000000004
এটি Java bug নয়।
এটি binary floating-point representation-এর limitation।
Why 0.1 Is Not Exact
Decimal 0.1 binary representation-এ repeating fraction হয়।
যেমন decimal system-এ:
1 / 3 = 0.333333...
একইভাবে কিছু decimal number binary-তে exactভাবে represent করা যায় না।
তাই approximation store করা হয়।
Floating-Point Equality Problem
Avoid:
double result = 0.1 + 0.2;
if (result == 0.3) {
System.out.println("Equal");
}
Condition false হতে পারে।
Approximate comparison:
double result = 0.1 + 0.2;
double expected = 0.3;
double tolerance = 0.000001;
if (Math.abs(result - expected) < tolerance) {
System.out.println("Approximately equal");
}
এই technique advanced comparison-এর basic idea।
Should double Be Used for Money?
Financial value-এর জন্য float বা double সাধারণত safe choice নয়, যখন exact decimal calculation প্রয়োজন।
Problem:
double balance = 0.1 + 0.2;
Exact currency calculation-এর জন্য common approach:
- Integer minor unit ব্যবহার করা
BigDecimalব্যবহার করা
Minor unit example:
long priceInCents = 499_050L;
এখানে:
499,050 cents = 4,990.50
অথবা Java-এর BigDecimal:
BigDecimal price = new BigDecimal("4990.50");
BigDecimal primitive type নয়। এটি একটি class এবং future course-এ শেখানো হবে।
Scientific Notation
খুব বড় বা ছোট floating-point value scientific notation-এ লেখা যায়।
double speedOfLight = 3.0E8;
মানে:
3.0 × 10⁸
Small value:
double tinyValue = 1.5E-6;
মানে:
0.0000015
Special Floating-Point Values
Floating-point type কিছু special value represent করতে পারে।
Positive Infinity
double result = 1.0 / 0.0;
System.out.println(result);
Output:
Infinity
Negative Infinity
double result = -1.0 / 0.0;
Output:
-Infinity
NaN
NaN মানে:
Not a Number
Example:
double result = 0.0 / 0.0;
System.out.println(result);
Output:
NaN
Check করতে:
Double.isNaN(result);
Integer Division by Zero vs Floating-Point Division by Zero
Integer:
int result = 10 / 0;
Runtime exception হবে।
Floating-point:
double result = 10.0 / 0.0;
Output:
Infinity
Behaviour আলাদা।
The char Type
char একটি single character represent করে।
Example:
char grade = 'A';
char symbol = '#';
char digit = '7';
char letter = 'ক';
char literal single quotation mark ব্যবহার করে:
' '
char vs String
Character:
char grade = 'A';
String:
String grade = "A";
Difference:
charএকটি characterStringzero, one বা multiple character-এর sequence
Example:
char firstLetter = 'J';
String language = "Java";
Invalid char
Invalid:
char language = 'Java';
একটি char literal-এর মধ্যে একটি character থাকতে হয়।
Correct:
char firstLetter = 'J';
অথবা:
String language = "Java";
Unicode and char
Java char UTF-16 code unit represent করে।
Example:
char copyrightSymbol = '\u00A9';
Output:
©
Bangla character:
char banglaLetter = 'ক';
Output:
ক
Unicode Escape
Unicode escape syntax:
\uXXXX
Example:
char letter = '\u0041';
\u0041 হলো A।
System.out.println(letter);
Output:
A
Escape Characters with char
New line:
char newLine = '\n';
Tab:
char tab = '\t';
Single quote:
char singleQuote = '\'';
Backslash:
char backslash = '\\';
char Is Numeric Internally
char একটি numeric Unicode code unit represent করে।
Example:
char letter = 'A';
System.out.println((int) letter);
Output:
65
A-এর Unicode value 65।
Character Arithmetic
char letter = 'A';
letter = (char) (letter + 1);
System.out.println(letter);
Output:
B
Casting পরবর্তী lesson-এ বিস্তারিত শেখানো হবে।
Limitation of char
সব visible Unicode character একটি single char-এ fit করে না।
কিছু emoji বা supplementary Unicode character দুইটি UTF-16 code unit ব্যবহার করে।
Example:
String emoji = "😀";
এটি String হিসেবে রাখা উচিত।
সব human-visible symbol-এর জন্য char যথেষ্ট—এমন নয়।
Beginner হিসেবে মনে রাখুন:
charএকটি UTF-16 code unit represent করে, সবসময় complete visible Unicode character নয়।
The boolean Type
boolean logical state represent করে।
Possible values:
true
false
Example:
boolean isAvailable = true;
boolean paymentCompleted = false;
boolean hasPermission = true;
Boolean Literals
Valid:
true
false
Invalid:
boolean isActive = "true";
কারণ "true" একটি String।
Correct:
boolean isActive = true;
Boolean Is Not an Integer
কিছু programming language-এ 0 বা 1 boolean-এর মতো ব্যবহার করা যায়।
Java-তে:
boolean isActive = 1;
Invalid।
Correct:
boolean isActive = true;
Boolean Expressions
Comparison result একটি boolean value।
int age = 20;
boolean isAdult = age >= 18;
এখানে:
age >= 18
Result:
true
Output:
System.out.println(isAdult);
true
More Boolean Examples
int score = 80;
boolean isPassed = score >= 40;
boolean hasPerfectScore = score == 100;
boolean needsImprovement = score < 60;
Output:
System.out.println(isPassed);
System.out.println(hasPerfectScore);
System.out.println(needsImprovement);
true
false
false
Boolean Naming
Boolean variable-এর নাম meaningful question-এর মতো হওয়া উচিত।
Good:
isActive
isAvailable
hasPermission
canEnroll
shouldRetry
paymentCompleted
Poor:
status
flag
value
check
Primitive vs Reference Types
Primitive variable basic value represent করে।
int age = 30;
boolean isActive = true;
Reference type variable একটি object-এর reference রাখে।
String name = "Sakib";
String primitive নয়।
এটি একটি class।
Primitive Examples
int age = 30;
double price = 99.50;
char grade = 'A';
boolean passed = true;
Reference Type Examples
String courseName = "Java Foundation";
Student student = new Student();
int[] marks = {80, 90, 85};
Primitive Value Copying
Primitive variable assign করলে value copy হয়।
int originalScore = 80;
int copiedScore = originalScore;
originalScore = 90;
System.out.println(originalScore);
System.out.println(copiedScore);
Output:
90
80
copiedScore independent copied value রাখে।
Default Values
Class field primitive type-এর default value থাকে।
| Type | Default Value |
|---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0F |
double | 0.0 |
char | '\u0000' |
boolean | false |
Example:
public class Student {
int age;
boolean active;
}
Object তৈরি হলে field defaultভাবে:
age = 0
active = false
Local Variables Have No Automatic Default
Local variable:
public static void main(String[] args) {
int age;
System.out.println(age);
}
Compile করবে না।
Local variable use করার আগে initialize করতে হবে।
Correct:
int age = 0;
Numeric Promotion
Arithmetic-এর সময় ছোট integer type অনেক ক্ষেত্রে int-এ promote হয়।
Example:
byte firstNumber = 10;
byte secondNumber = 20;
int total = firstNumber + secondNumber;
Valid।
কিন্তু:
byte total = firstNumber + secondNumber;
Compile নাও করতে পারে।
কারণ expression result int হিসেবে promote হয়েছে।
Casting future lesson-এ শেখানো হবে।
Mixed-Type Arithmetic
int quantity = 3;
double price = 99.50;
double total = quantity * price;
Result double।
System.out.println(total);
Output:
298.5
Expression-এ wider type থাকলে result wider type-এ promote হতে পারে।
Choosing the Correct Data Type
Data type নির্বাচন করার সময় ভাবুন:
- Value পূর্ণসংখ্যা নাকি decimal?
- Possible range কত?
- Exact precision প্রয়োজন কি?
- Memory sensitivity আছে কি?
- External API বা database type কী?
- Value logical state কি?
- Single character নাকি text?
- Future growth কত হতে পারে?
Age
int age = 30;
byte range যথেষ্ট হলেও int বেশি conventional এবং convenient।
Student Count
int studentCount = 5000;
Large Population
long population = 8_000_000_000L;
Average Mark
double averageMark = 81.67;
Grade
char grade = 'A';
Course Availability
boolean isAvailable = true;
Money in Minor Units
long coursePriceInPaisa = 499_000L;
Exact monetary value-এর জন্য integer minor unit useful।
Using Wrapper Constants to See Ranges
Java wrapper classes primitive range expose করে।
System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
Possible output:
-128
127
-32768
32767
-2147483648
2147483647
-9223372036854775808
9223372036854775807
Wrapper Classes Preview
প্রতিটি primitive type-এর corresponding wrapper class আছে।
| Primitive | Wrapper |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Wrapper class object, utility method এবং collection support দেয়।
এগুলো future lesson-এ বিস্তারিত শেখানো হবে।
Example: Student Result
public class Main {
public static void main(String[] args) {
String studentName = "Sakib";
int mathematicsMark = 80;
int englishMark = 90;
int scienceMark = 85;
int totalMarks =
mathematicsMark
+ englishMark
+ scienceMark;
double averageMark = totalMarks / 3.0;
char grade = 'A';
boolean isPassed = true;
System.out.println("Student: " + studentName);
System.out.println("Total: " + totalMarks);
System.out.println("Average: " + averageMark);
System.out.println("Grade: " + grade);
System.out.println("Passed: " + isPassed);
}
}
Output:
Student: Sakib
Total: 255
Average: 85.0
Grade: A
Passed: true
Example: Course Information
public class Main {
public static void main(String[] args) {
int lessonCount = 21;
long enrolledLearnerCount = 5_000_000_000L;
double coursePrice = 4990.50;
char courseLevel = 'B';
boolean enrollmentOpen = true;
System.out.println("Lesson count: " + lessonCount);
System.out.println("Learners: " + enrolledLearnerCount);
System.out.println("Price: " + coursePrice);
System.out.println("Level: " + courseLevel);
System.out.println("Enrollment open: " + enrollmentOpen);
}
}
Example: Product Order
public class Main {
public static void main(String[] args) {
int productPriceInPaisa = 50_000;
int quantity = 3;
int deliveryChargeInPaisa = 8_000;
long subtotalInPaisa =
(long) productPriceInPaisa * quantity;
long totalInPaisa =
subtotalInPaisa + deliveryChargeInPaisa;
boolean paymentCompleted = false;
System.out.println("Subtotal in paisa: " + subtotalInPaisa);
System.out.println("Delivery charge in paisa: " + deliveryChargeInPaisa);
System.out.println("Total in paisa: " + totalInPaisa);
System.out.println("Payment completed: " + paymentCompleted);
}
}
Casting syntax future lesson-এ বিস্তারিত শেখানো হবে।
Common Error: Value Outside byte Range
Wrong:
byte value = 200;
Correct option:
short value = 200;
অথবা:
int value = 200;
Common Error: Missing L
Wrong:
long population = 8_000_000_000;
Correct:
long population = 8_000_000_000L;
Common Error: Missing F
Wrong:
float price = 99.50;
Correct:
float price = 99.50F;
অথবা double ব্যবহার করুন:
double price = 99.50;
Common Error: Integer Division
double average = 5 / 2;
Output:
2.0
কারণ 5 / 2 আগে integer division করে 2 হয়েছে, তারপর double-এ assign হয়েছে।
Correct:
double average = 5.0 / 2;
অথবা:
double average = 5 / 2.0;
Output:
2.5
Common Error: Using double for Exact Money
Risky:
double amount = 0.1 + 0.2;
Better options:
long amountInCents = 30L;
অথবা:
BigDecimal amount = new BigDecimal("0.30");
Common Error: Wrong Quotes for char
Wrong:
char grade = "A";
Correct:
char grade = 'A';
Common Error: Multiple Characters in char
Wrong:
char language = 'Java';
Correct:
String language = "Java";
Common Error: Number as Boolean
Wrong:
boolean active = 1;
Correct:
boolean active = true;
Common Error: Text as Boolean
Wrong:
boolean active = "true";
Correct:
boolean active = true;
Common Error: Silent Overflow
int total = 2_000_000_000 + 1_000_000_000;
System.out.println(total);
Expected mathematical result:
3,000,000,000
কিন্তু int range-এর বাইরে।
Better:
long total = 2_000_000_000L + 1_000_000_000L;
Output:
3000000000
Literal Type Matters
Wrong:
long total = 2_000_000_000 + 1_000_000_000;
যদিও result variable long, দুই operand প্রথমে int হিসেবে add হতে পারে এবং overflow ঘটতে পারে।
Correct:
long total = 2_000_000_000L + 1_000_000_000L;
কমপক্ষে একটি operand long করলে calculation long arithmetic ব্যবহার করবে।
Primitive Type Selection Guide
| Requirement | Suggested Type |
|---|---|
| সাধারণ whole number | int |
| খুব বড় whole number | long |
| General decimal | double |
| Memory-sensitive approximate decimal | float |
| Single UTF-16 code unit | char |
| True/false state | boolean |
| Exact money | long minor units বা BigDecimal |
| Raw binary 8-bit data | byte |
Important Terms
Primitive Data Type
Java-এর built-in basic data type, যা simple value represent করে।
Integer
Decimal অংশ ছাড়া পূর্ণসংখ্যা।
Floating-Point Number
Fractional বা decimal value represent করা approximate numeric value।
Signed Number
Positive এবং negative value represent করতে পারে এমন numeric type।
Range
একটি type যে minimum এবং maximum value রাখতে পারে।
Precision
একটি numeric type কত digit পর্যন্ত reliableভাবে represent করতে পারে।
Literal
Source code-এ সরাসরি লেখা value।
Example:
100
99.50
'A'
true
Overflow
Maximum representable value-এর চেয়ে বড় result হলে value wrap করা।
Underflow
Minimum representable value-এর নিচে গেলে value wrap করা।
Integer Division
দুটি integer divide করলে fractional অংশ বাদ যাওয়া।
Floating-Point Precision Error
Binary representation-এর কারণে decimal value approximate হওয়া।
NaN
Not a Number।
Infinity
Floating-point calculation-এর special infinite value।
Unicode
Character represent করার international encoding standard।
Primitive Promotion
Arithmetic-এর সময় ছোট type wider type-এ convert হওয়া।
Wrapper Class
Primitive value-এর object representation এবং utility API প্রদানকারী class।
Practice Exercise 1: Choose the Correct Type
নিচের value-এর জন্য suitable primitive type লিখুন:
- Student age
- Course lesson count
- পৃথিবীর population
- Average mark
- একটি letter grade
- User active কি না
- File size in bytes
- Temperature
- Maximum login attempts
- একটি Bangla letter
Practice Exercise 2: Identify the Type
প্রতিটি literal-এর সাধারণ type লিখুন:
100
100L
10.5
10.5F
'A'
true
Practice Exercise 3: Find the Invalid Declarations
নিচের declarationগুলোর মধ্যে কোনগুলো invalid?
byte age = 25;
byte value = 200;
short roomNumber = 1200;
int population = 8_000_000_000;
long population = 8_000_000_000L;
float price = 99.50;
float temperature = 36.5F;
double average = 81.67;
char grade = "A";
char letter = 'A';
boolean active = 1;
boolean available = true;
Invalid declarationগুলো correct করুন।
Practice Exercise 4: Integer Division
Output predict করুন:
int firstResult = 5 / 2;
double secondResult = 5 / 2;
double thirdResult = 5.0 / 2;
double fourthResult = 5 / 2.0;
System.out.println(firstResult);
System.out.println(secondResult);
System.out.println(thirdResult);
System.out.println(fourthResult);
Practice Exercise 5: Fix the Average
নিচের code expected decimal average দিচ্ছে না:
int totalMarks = 250;
int subjectCount = 3;
double average = totalMarks / subjectCount;
System.out.println(average);
Code fix করুন, যাতে decimal result পাওয়া যায়।
Practice Exercise 6: Detect Overflow
নিচের code-এর output কী হতে পারে?
int value = Integer.MAX_VALUE;
value = value + 1;
System.out.println(value);
Explain করুন কেন এমন output হয়।
Practice Exercise 7: Prevent Overflow
নিচের calculation correct করুন:
long total = 2_000_000_000 + 2_000_000_000;
Expected result:
4000000000
Practice Exercise 8: Numeric Underscores
নিচের valueগুলো underscore ব্যবহার করে readableভাবে লিখুন:
- One million
- Eight billion as
long - 4,990.50
- 100 million
- 9,000,000,000,000
Practice Exercise 9: Character and String
প্রতিটি requirement-এর জন্য char নাকি String ব্যবহার করবেন?
- Grade
A - Language name
"Java" - Bangla letter
ক - Student name
- Currency symbol
$ - Sentence
- Emoji
- Yes/No code
Y
Practice Exercise 10: Boolean Variables
নিচের requirement অনুযায়ী meaningful boolean variable তৈরি করুন:
- User active
- Payment completed
- Learner can enroll
- Course has certificate
- System should retry
- Email verified
Practice Exercise 11: Floating-Point Precision
নিচের code run করুন:
double result = 0.1 + 0.2;
System.out.println(result);
System.out.println(result == 0.3);
Explain করুন:
- Output কেন exactly
0.3নাও হতে পারে? - Equality comparison false কেন হতে পারে?
- Money-এর জন্য এই behaviour কেন problematic?
Practice Exercise 12: Primitive Value Copy
Output predict করুন:
int firstScore = 80;
int secondScore = firstScore;
firstScore = 90;
System.out.println(firstScore);
System.out.println(secondScore);
Explain করুন কেন secondScore change হয়নি।
Practice Exercise 13: Default Values
নিচের class দেখুন:
public class Student {
int age;
double average;
char grade;
boolean active;
}
প্রতিটি field-এর default value লিখুন।
Practice Exercise 14: Local Variable Initialization
নিচের code compile করবে কি না?
public class Main {
public static void main(String[] args) {
int score;
System.out.println(score);
}
}
Compile না করলে reason এবং correct version লিখুন।
Practice Exercise 15: Student Result Program
একটি program লিখুন যেখানে থাকবে:
- Student name as
String - Mathematics mark as
int - English mark as
int - Science mark as
int - Total mark as
int - Average mark as
double - Grade as
char - Passed status as
boolean
Output format:
Student: Sakib
Mathematics: 80
English: 90
Science: 85
Total: 255
Average: 85.0
Grade: A
Passed: true
Practice Exercise 16: Course Statistics Program
নিচের data ব্যবহার করুন:
Lesson count: 21
Enrolled learner count: 5,000,000,000
Completion percentage: 72.5
Course level code: B
Enrollment open: true
প্রতিটি value-এর জন্য suitable type নির্বাচন করে complete Java program লিখুন।
Practice Exercise 17: Exact Money Representation
Course price:
BDT 4,990.50
Valueটি long minor unit ব্যবহার করে represent করুন।
ধরা যাক:
1 BDT = 100 paisa
উত্তর দিন:
- Paisa-তে value কত?
- Variable-এর type কী?
- Variable-এর meaningful name কী?
doubleব্যবহার না করার একটি কারণ কী?
Practice Exercise 18: Wrapper Range Constants
একটি Java program লিখুন, যা print করবে:
Byte.MIN_VALUEByte.MAX_VALUEShort.MIN_VALUEShort.MAX_VALUEInteger.MIN_VALUEInteger.MAX_VALUELong.MIN_VALUELong.MAX_VALUE
Output observe করুন।
Practice Exercise 19: Special Floating-Point Values
নিচের code-এর output predict করুন:
double positiveInfinity = 1.0 / 0.0;
double negativeInfinity = -1.0 / 0.0;
double notANumber = 0.0 / 0.0;
System.out.println(positiveInfinity);
System.out.println(negativeInfinity);
System.out.println(notANumber);
Practice Exercise 20: Explain in Your Own Words
নিজের ভাষায় উত্তর দিন:
- Primitive data type কী?
intএবংlong-এর পার্থক্য কী?longliteral-এর শেষেLকেন লাগে?floatliteral-এর শেষেFকেন লাগে?floatএবংdouble-এর পার্থক্য কী?- Integer division কী?
- Overflow কী?
- Floating-point precision error কী?
- Money-এর জন্য
doublerisky কেন? charএবংString-এর পার্থক্য কী?booleanকী ধরনের value রাখে?- Local primitive variable-এর default value থাকে কি?
- Primitive এবং reference type-এর basic পার্থক্য কী?
Knowledge Check
Question 1
Java-তে primitive data type কয়টি?
Question 2
Java-এর আটটি primitive type কী?
Question 3
Integer primitive type কী কী?
Question 4
Floating-point primitive type কী কী?
Question 5
সাধারণ whole number-এর জন্য default choice কোন type?
Question 6
int range-এর চেয়ে বড় value-এর জন্য কোন type ব্যবহার করা হয়?
Question 7
long literal-এর recommended suffix কী?
Question 8
Decimal literal defaultভাবে কোন type?
Question 9
float literal-এর suffix কী?
Question 10
সাধারণ decimal calculation-এর জন্য float নাকি double বেশি common?
Question 11
5 / 2 integer calculation-এর result কী?
Question 12
5.0 / 2-এর result কী?
Question 13
Integer overflow কী?
Question 14
0.1 + 0.2 exactly 0.3 নাও হতে পারে কেন?
Question 15
Exact financial calculation-এর জন্য কী ব্যবহার করা যেতে পারে?
Question 16
char literal কোন quotation mark ব্যবহার করে?
Question 17
String literal কোন quotation mark ব্যবহার করে?
Question 18
boolean-এর possible value কী কী?
Question 19
Java-তে 1 কি valid boolean value?
Question 20
Class field int-এর default value কী?
Question 21
Class field boolean-এর default value কী?
Question 22
Local variable কি use করার আগে initialize করতে হয়?
Question 23
String কি primitive type?
Question 24
Primitive assignment-এর সময় সাধারণত কী copy হয়?
Question 25
byte + byte arithmetic result সাধারণত কোন type-এ promote হয়?
Knowledge Check Answers
Answer 1
Java-তে আটটি primitive data type রয়েছে।
Answer 2
Java-এর primitive types:
byte
short
int
long
float
double
char
boolean
Answer 3
Integer primitive types:
byte
short
int
long
Answer 4
Floating-point primitive types:
float
double
Answer 5
সাধারণ whole number-এর জন্য সাধারণত int ব্যবহার করা হয়।
Answer 6
long ব্যবহার করা হয়।
Answer 7
Recommended suffix:
L
Answer 8
Decimal literal defaultভাবে double।
Answer 9
float literal suffix:
F
Answer 10
সাধারণ decimal calculation-এর জন্য double বেশি common।
Answer 11
2
কারণ এটি integer division।
Answer 12
2.5
Answer 13
Result type-এর maximum range ছাড়িয়ে গেলে value wrap হওয়াকে integer overflow বলা হয়।
Answer 14
double binary floating-point representation ব্যবহার করে এবং সব decimal value binary-তে exactly represent করা যায় না।
Answer 15
Exact financial calculation-এর জন্য ব্যবহার করা যেতে পারে:
- Integer minor units, যেমন
long BigDecimal
Answer 16
char single quotes ব্যবহার করে:
'A'
Answer 17
String double quotes ব্যবহার করে:
"Java"
Answer 18
true
false
Answer 19
না। Java-তে 1 valid boolean value নয়।
Answer 20
Class field int-এর default value:
0
Answer 21
Class field boolean-এর default value:
false
Answer 22
হ্যাঁ। Local variable use করার আগে initialize করতে হয়।
Answer 23
না। String একটি reference type এবং class।
Answer 24
Primitive assignment-এর সময় value copy হয়।
Answer 25
ছোট integer type arithmetic-এর result সাধারণত int-এ promote হয়।
Lesson Summary
এই lesson-এ আমরা শিখেছি:
- Java-তে আটটি primitive data type আছে
byte,short,intএবংlonginteger value রাখেfloatএবংdoubledecimal value রাখেcharএকটি UTF-16 code unit represent করেbooleantrueবাfalsevalue রাখে- সাধারণ integer-এর জন্য
intdefault choice - বড় integer-এর জন্য
longব্যবহার করা হয় longliteral-এর শেষেLব্যবহার করা হয়- Decimal literal defaultভাবে
double floatliteral-এর শেষেFব্যবহার করা হয়doublefloat-এর তুলনায় বেশি precision দেয়- Integer division decimal অংশ বাদ দেয়
- Integer overflow silentভাবে wrong value তৈরি করতে পারে
- Numeric literal readable করতে underscore ব্যবহার করা যায়
- Floating-point value approximate
0.1 + 0.2exact0.3নাও হতে পারে- Exact money calculation-এর জন্য
longminor unit বাBigDecimalভালো charsingle quotes এবংStringdouble quotes ব্যবহার করে- সব visible Unicode symbol একটি single
char-এ fit করে না - Java boolean integer নয়
- Class field primitive default value পায়
- Local variable use করার আগে initialize করতে হয়
- Primitive variable assign করলে value copy হয়
- Arithmetic-এর সময় type promotion হতে পারে
- সঠিক data type নির্বাচন correctness এবং maintainability-এর জন্য গুরুত্বপূর্ণ
Next Lesson
পরবর্তী lesson-এ আমরা Java-তে text value নিয়ে কাজ করব।
আমরা শিখব:
Stringকী- String literal
- String variable
- Empty String এবং
null - String concatenation
- String length
- Character access
- Common String methods
- String equality
- String immutability
- Escape sequences
- Text block
Stringএবংchar-এর পার্থক্য