Programming and Java Fundamentals
Java Syntax and Coding Conventions
আপনি একটি free preview lesson দেখছেন।
Lesson Overview
Programming language ব্যবহার করে code লেখার জন্য সেই language-এর নির্দিষ্ট rule follow করতে হয়।
Java code কীভাবে লিখতে হবে, কোন symbol কোথায় ব্যবহার করতে হবে, identifier কীভাবে নাম দিতে হবে এবং code কীভাবে format করতে হবে—এসব Java syntax-এর অংশ।
Syntax ভুল হলে compiler program compile করতে পারবে না।
অন্যদিকে, code technically valid হলেও naming এবং formatting খারাপ হলে code পড়া, বোঝা এবং maintain করা কঠিন হয়ে যায়।
এই lesson-এ আমরা শিখব:
- Java syntax কী
- Java case-sensitive হওয়ার অর্থ
- Java keyword কী
- Identifier কী
- Identifier naming rules
- Java naming conventions
- Semicolon, braces এবং parentheses-এর ব্যবহার
- Whitespace এবং indentation
- Java comments
- Code formatting
- Readable এবং maintainable Java code লেখার basic practice
Learning Objectives
এই lesson শেষ করার পর আপনি পারবেন:
- Java syntax কী, তা ব্যাখ্যা করতে
- Case-sensitive identifier শনাক্ত করতে
- Java keyword এবং identifier-এর পার্থক্য বুঝতে
- Valid এবং invalid identifier আলাদা করতে
- Class, variable, method এবং constant-এর naming convention follow করতে
- Semicolon, braces এবং parentheses সঠিকভাবে ব্যবহার করতে
- Single-line, multi-line এবং Javadoc comment লিখতে
- Java code readableভাবে format করতে
- Common syntax error শনাক্ত এবং fix করতে
What Is Syntax?
Syntax হলো programming language-এর grammatical rule।
যেমন একটি human language-এ sentence লেখার rule থাকে, তেমনি Java code লেখারও rule আছে।
Java compiler expect করে code নির্দিষ্ট structure follow করবে।
উদাহরণ:
int age = 30;
এটি valid Java statement।
কিন্তু নিচের code valid নয়:
int age 30;
এখানে assignment operator = নেই।
আরেকটি invalid example:
int age = 30
এখানে semicolon নেই।
Correct:
int age = 30;
Syntax Error কী?
Code Java language-এর rule follow না করলে syntax error হয়।
উদাহরণ:
System.out.println("Hello Java")
Possible compiler error:
';' expected
Correct code:
System.out.println("Hello Java");
Syntax error সাধারণত compilation-এর সময় পাওয়া যায়।
Java Is Case-Sensitive
Java uppercase এবং lowercase letter-কে আলাদা হিসেবে বিবেচনা করে।
উদাহরণ:
Main
main
MAIN
এই তিনটি আলাদা identifier।
Correct Java class:
public class Main {
}
নিচের class-এর নাম আলাদা:
public class main {
}
Technically main class name হিসেবে ব্যবহার করা সম্ভব হলেও এটি Java class naming convention follow করে না।
Case-Sensitive Method Names
Correct:
System.out.println("Hello");
Incorrect:
system.out.println("Hello");
Incorrect:
System.Out.Println("Hello");
কারণ correct identifiers হলো:
System
out
println
Case-Sensitive Variables
int age = 30;
int Age = 40;
int AGE = 50;
Java-এর কাছে এগুলো তিনটি আলাদা variable।
System.out.println(age);
System.out.println(Age);
System.out.println(AGE);
Output:
30
40
50
যদিও এই code valid, একই meaning-এর variable শুধু capitalization পরিবর্তন করে তৈরি করা উচিত নয়।
এতে code confusing হয়ে যায়।
Avoid:
int price = 100;
int Price = 200;
int PRICE = 300;
Java Keywords
Keyword হলো Java language-এর reserved word।
Keyword-এর predefined meaning থাকে।
Common Java keyword:
public
private
protected
class
static
void
int
double
boolean
if
else
switch
case
for
while
do
break
continue
return
new
this
super
extends
implements
interface
abstract
final
try
catch
finally
throw
throws
package
import
Example:
public class Main {
public static void main(String[] args) {
int age = 30;
if (age >= 18) {
System.out.println("Adult");
}
}
}
এখানে keyword:
public
class
static
void
int
if
Keyword Identifier হিসেবে ব্যবহার করা যায় না
Invalid:
int class = 10;
Invalid:
String public = "Java";
Invalid:
boolean static = true;
কারণ class, public এবং static Java keyword।
true, false, and null
নিচের values keyword-এর মতো reserved literal:
true
false
null
Variable name হিসেবে এগুলো ব্যবহার করা যায় না।
Invalid:
boolean true = false;
Invalid:
String null = "empty";
Identifiers
Identifier হলো programmer-এর দেওয়া নাম।
Java program-এ identifier ব্যবহার করা হয়:
- Class-এর নাম দিতে
- Method-এর নাম দিতে
- Variable-এর নাম দিতে
- Parameter-এর নাম দিতে
- Package-এর নাম দিতে
- Interface-এর নাম দিতে
- Constant-এর নাম দিতে
Example:
public class Student {
String studentName;
int age;
void displayInformation() {
System.out.println(studentName);
}
}
এখানে identifier:
Student
studentName
age
displayInformation
Identifier Naming Rules
Java identifier লেখার কিছু mandatory rule আছে।
Rule 1: Letter দিয়ে শুরু করা যায়
Valid:
int age;
String name;
double price;
Rule 2: Underscore দিয়ে শুরু করা যায়
Valid:
int _age;
String _name;
তবে সাধারণ application code-এ leading underscore avoid করা ভালো, যদি specific convention না থাকে।
Rule 3: Dollar Sign দিয়ে শুরু করা যায়
Valid:
int $price;
তবে normal Java code-এ $ ব্যবহার avoid করা উচিত।
Generated code বা special tool এটি ব্যবহার করতে পারে।
Rule 4: Number দিয়ে শুরু করা যায় না
Invalid:
int 1stNumber;
Invalid:
String 2ndName;
Correct:
int firstNumber;
String secondName;
Rule 5: প্রথম Character-এর পরে Number থাকতে পারে
Valid:
int number1;
int student2;
String course2026;
Rule 6: Space ব্যবহার করা যায় না
Invalid:
int student age;
Correct:
int studentAge;
Rule 7: Hyphen ব্যবহার করা যায় না
Invalid:
int student-age;
Compiler এটিকে subtraction expression হিসেবে interpret করার চেষ্টা করতে পারে।
Correct:
int studentAge;
Rule 8: Java Keyword ব্যবহার করা যায় না
Invalid:
int for;
Correct:
int loopCount;
Rule 9: Identifier Case-Sensitive
int total;
int Total;
এগুলো আলাদা identifier।
Valid Identifier Examples
age
studentName
coursePrice
totalMarks
isAvailable
number1
_internalValue
$generatedValue
Invalid Identifier Examples
1student
student name
student-name
class
public
total@
course.price
Valid and Recommended Are Not the Same
কিছু identifier technically valid হলেও recommended নয়।
Valid but poor:
int a;
int x1;
String n;
double p;
এগুলো ছোট scope-এর special case ছাড়া meaning পরিষ্কার করে না।
Better:
int studentAge;
int totalStudents;
String courseName;
double productPrice;
আরেকটি technically valid example:
int _student_age;
Better Java convention:
int studentAge;
Naming Conventions
Naming convention compiler-এর mandatory rule নয়।
কিন্তু professional Java codebase-এ consistent naming অত্যন্ত গুরুত্বপূর্ণ।
Java-তে common naming styles:
- PascalCase
- camelCase
- UPPER_SNAKE_CASE
- lowercase package names
PascalCase
PascalCase-এ প্রতিটি word capital letter দিয়ে শুরু হয়।
Example:
Student
BankAccount
GradeCalculator
CourseEnrollment
PaymentProcessor
Java-তে সাধারণত PascalCase ব্যবহার করা হয়:
- Class
- Interface
- Enum
- Record
Class Naming Convention
Class name noun বা meaningful concept হওয়া উচিত।
Good:
public class Student {
}
public class BankAccount {
}
public class GradeCalculator {
}
Avoid:
public class student {
}
public class grade_calculator {
}
public class Data1 {
}
Class Name Should Describe Responsibility
Poor:
public class Manager {
}
Manager কী manage করে, তা clear নয়।
Better:
public class CourseManager {
}
অথবা:
public class EnrollmentManager {
}
তবে Manager, Helper, Util-এর মতো generic suffix অতিরিক্ত ব্যবহার করা উচিত নয়।
Class-এর clear responsibility থাকা উচিত।
camelCase
camelCase-এ প্রথম word lowercase দিয়ে শুরু হয় এবং পরবর্তী word capital letter দিয়ে শুরু হয়।
Example:
studentName
coursePrice
calculateTotal
isAvailable
totalNumberOfStudents
Java-তে camelCase সাধারণত ব্যবহার করা হয়:
- Variable
- Method
- Parameter
- Field
Variable Naming Convention
Good:
int studentAge;
String courseName;
double productPrice;
boolean isAvailable;
Avoid:
int StudentAge;
String course_name;
double p;
boolean flag;
Boolean Variable Naming
Boolean variable এমনভাবে নাম দেওয়া ভালো, যাতে true বা false condition বোঝা যায়।
Good:
boolean isActive;
boolean hasPermission;
boolean canEnroll;
boolean shouldRetry;
boolean paymentCompleted;
Less clear:
boolean status;
boolean value;
boolean flag;
Example:
boolean isAdult = age >= 18;
Method Naming Convention
Method name সাধারণত verb বা action দিয়ে শুরু হয়।
Good:
calculateTotal()
displayStudentInformation()
createAccount()
sendEmail()
validatePassword()
findCourseById()
Avoid:
total()
student()
account()
doThing()
processData()
Method-এর নাম দেখে তার behaviour বোঝা উচিত।
Method Name Examples
static void displayWelcomeMessage() {
System.out.println("Welcome");
}
static int calculateTotal(int price, int quantity) {
return price * quantity;
}
static boolean isEligibleToVote(int age) {
return age >= 18;
}
Parameter Naming Convention
Parameter name input-এর meaning প্রকাশ করা উচিত।
Good:
static int calculateTotal(int price, int quantity) {
return price * quantity;
}
Avoid:
static int calculateTotal(int a, int b) {
return a * b;
}
Mathematical short method ছাড়া meaningful names বেশি readable।
UPPER_SNAKE_CASE
Constant-এর নাম সাধারণত uppercase letter এবং underscore ব্যবহার করে লেখা হয়।
Example:
MAXIMUM_ATTEMPTS
DEFAULT_PAGE_SIZE
MINIMUM_AGE
TAX_RATE
Java constant সাধারণত static final দিয়ে declare করা হয়।
Example:
static final int MINIMUM_VOTING_AGE = 18;
static final int MAXIMUM_LOGIN_ATTEMPTS = 5;
static final double TAX_RATE = 0.15;
final এবং constant future lesson-এ বিস্তারিত শেখানো হবে।
Poor Constant Naming
Avoid:
static final int minimumVotingAge = 18;
এটি compile করবে, কিন্তু conventional constant style নয়।
Preferred:
static final int MINIMUM_VOTING_AGE = 18;
Package Naming Convention
Package name সাধারণত lowercase-এ লেখা হয়।
Example:
package com.liveklass.foundation;
package io.liveklass.course;
Avoid:
package Com.LiveKlass.Foundation;
Package name-এর মধ্যে space বা hyphen ব্যবহার করা যায় না।
Naming Summary
| Element | Convention | Example |
|---|---|---|
| Class | PascalCase | GradeCalculator |
| Interface | PascalCase | PaymentProcessor |
| Method | camelCase | calculateTotal() |
| Variable | camelCase | studentName |
| Parameter | camelCase | coursePrice |
| Constant | UPPER_SNAKE_CASE | MAXIMUM_ATTEMPTS |
| Package | lowercase | com.liveklass.course |
Meaningful Names
Code মানুষের পড়ার জন্যও লেখা হয়।
Meaningful name code explain করতে সাহায্য করে।
Poor:
int x = 80;
int y = 90;
int z = x + y;
Better:
int mathematicsMark = 80;
int englishMark = 90;
int totalMarks = mathematicsMark + englishMark;
দ্বিতীয় version বেশি code হলেও meaning পরিষ্কার।
Avoid Unnecessary Abbreviations
Avoid:
int stdCnt;
String crsNm;
double prdPrc;
Better:
int studentCount;
String courseName;
double productPrice;
কিছু common abbreviation acceptable হতে পারে:
id
url
api
http
json
তবুও project convention follow করা উচিত।
Avoid Misleading Names
Bad:
int studentName = 30;
studentName দেখে text expectation তৈরি হয়, কিন্তু type int।
Better:
int studentAge = 30;
Bad:
boolean studentCount = true;
Better:
boolean hasStudents = true;
Semicolons
Java-তে বেশিরভাগ simple statement semicolon দিয়ে শেষ হয়।
;
Examples:
int age = 30;
String name = "Sakib";
age = 31;
System.out.println(name);
Missing Semicolon
Wrong:
int age = 30
Correct:
int age = 30;
Wrong:
System.out.println("Hello")
Correct:
System.out.println("Hello");
Multiple Statements on One Line
Technically valid:
int age = 30; String name = "Sakib"; System.out.println(name);
কিন্তু avoid করা উচিত।
Recommended:
int age = 30;
String name = "Sakib";
System.out.println(name);
এক line-এ একটি statement সাধারণত বেশি readable।
Empty Statement
একটি semicolon নিজেও empty statement হতে পারে।
Example:
;
Accidentally extra semicolon confusion তৈরি করতে পারে।
Example:
if (age >= 18);
{
System.out.println("Eligible");
}
এখানে if condition-এর পরে extra semicolon রয়েছে।
এর ফলে block condition-এর সঙ্গে properly যুক্ত নয়।
Correct:
if (age >= 18) {
System.out.println("Eligible");
}
Condition lesson-এ এই problem বিস্তারিত দেখা হবে।
Curly Braces
Curly braces:
{ }
Code block define করে।
ব্যবহার হয়:
- Class body
- Method body
- Condition block
- Loop block
- Constructor body
Example:
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Opening Brace Style
Java-তে common style:
public class Main {
}
Method:
static void displayMessage() {
}
Condition:
if (age >= 18) {
}
Opening brace declaration-এর একই line-এ রাখা হয়।
Matching Braces
প্রতিটি opening brace-এর একটি matching closing brace থাকতে হবে।
Correct:
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Wrong:
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
এখানে একটি closing brace missing।
Nested Braces
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Adult");
}
}
}
Structure:
Class block
└── Method block
└── If block
Indentation matching braces বুঝতে সাহায্য করে।
Parentheses
Parentheses:
( )
Java-তে বিভিন্ন কাজে ব্যবহৃত হয়।
Method Declaration
public static void main(String[] args)
Method Call
System.out.println("Hello");
Condition
if (age >= 18) {
}
Expression Grouping
int result = (10 + 20) * 2;
Parentheses operation order control করতে পারে।
Missing Parenthesis
Wrong:
System.out.println("Hello";
Correct:
System.out.println("Hello");
Wrong:
if age >= 18 {
}
Correct:
if (age >= 18) {
}
Square Brackets
Square brackets:
[ ]
Array-related syntax-এ ব্যবহার করা হয়।
Example:
String[] args
int[] marks;
Array element access:
marks[0]
Array future lesson-এ বিস্তারিত শেখানো হবে।
Double Quotes and Single Quotes
Double quotes String-এর জন্য:
String language = "Java";
Single quotes single character-এর জন্য:
char grade = 'A';
Wrong:
String language = 'Java';
Wrong:
char grade = "A";
Correct:
String language = "Java";
char grade = 'A';
Assignment Operator
Assignment operator:
=
Variable-এ value assign করতে ব্যবহৃত হয়।
int age = 30;
এখানে:
intdata typeageidentifier=assignment operator30value;statement ending
Assignment and Equality Are Different
Assignment:
age = 30;
Equality comparison:
age == 30
একটি = এবং দুটি ==-এর কাজ আলাদা।
Condition lesson-এ এটি বিস্তারিত শেখানো হবে।
Whitespace
Whitespace-এর মধ্যে রয়েছে:
- Space
- Tab
- New line
Java compiler সাধারণত unnecessary whitespace ignore করে।
এই code valid:
int age = 30;
এটিও valid:
int age = 30;
কিন্তু দ্বিতীয়টি readable নয়।
Whitespace Between Tokens
নিচের code invalid:
intstudentAge = 30;
Compiler intstudentAge-কে একটি identifier হিসেবে দেখবে।
Correct:
int studentAge = 30;
Keyword এবং identifier আলাদা করতে whitespace প্রয়োজন।
Whitespace Inside Strings
String-এর ভেতরের whitespace preserve হয়।
System.out.println("Hello Java");
Output:
Hello Java
System.out.println("Hello Java");
Output:
Hello Java
Quotation marks-এর ভেতরের space data-এর অংশ।
Indentation
Indentation nested code-এর structure দেখায়।
Recommended:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Adult");
}
}
}
Poor formatting:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Adult");
}
}
}
Compiler দ্বিতীয় code accept করতে পারে, কিন্তু মানুষের পড়ার জন্য কঠিন।
Four-Space Indentation
Java project-এ commonly nested level প্রতি চারটি space ব্যবহার করা হয়।
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Team বা project-specific formatting rule থাকলে সেটি follow করতে হবে।
Blank Lines
Blank line related code section আলাদা করে।
Readable:
public class Main {
public static void main(String[] args) {
String courseName = "Java Foundation";
int courseDuration = 8;
System.out.println(courseName);
System.out.println(courseDuration);
}
}
অতিরিক্ত blank line avoid করুন:
String courseName = "Java Foundation";
int courseDuration = 8;
Line Length
অত্যন্ত দীর্ঘ line পড়তে কঠিন।
Long:
System.out.println("The student has successfully completed the Java and Object-Oriented Programming Foundation course.");
String-এর ক্ষেত্রে long line কখনো acceptable হতে পারে।
Long expression readableভাবে break করা যায়:
int totalPrice =
firstProductPrice
+ secondProductPrice
+ thirdProductPrice;
Team formatter অনুযায়ী wrapping style আলাদা হতে পারে।
Comments
Comment code-এর behaviour explain, context provide বা documentation তৈরিতে ব্যবহার করা হয়।
Java-তে তিন ধরনের common comment:
- Single-line comment
- Multi-line comment
- Javadoc comment
Single-Line Comment
Single-line comment শুরু হয়:
//
Example:
// Print a welcome message
System.out.println("Welcome");
//-এর পর থেকে line-এর শেষ পর্যন্ত compiler ignore করে।
Inline Comment
int minimumAge = 18; // Minimum voting age
Inline comment technically valid।
তবে অতিরিক্ত inline comment code clutter করতে পারে।
Better হতে পারে meaningful constant:
static final int MINIMUM_VOTING_AGE = 18;
Multi-Line Comment
Multi-line comment শুরু হয়:
/*
শেষ হয়:
*/
Example:
/*
* This program demonstrates
* basic Java syntax.
*/
public class Main {
}
Compiler comment-এর ভেতরের content ignore করে।
Commenting Out Code
Temporaryভাবে code disable করতে comment ব্যবহার করা যায়।
// System.out.println("This will not run");
System.out.println("This will run");
Output:
This will run
তবে production code-এ দীর্ঘ unused commented code রেখে দেওয়া উচিত নয়।
Version control previous code preserve করতে পারে।
Javadoc Comment
Javadoc comment শুরু হয়:
/**
শেষ হয়:
*/
Example:
/**
* Calculates the total price.
*/
public class PriceCalculator {
}
Method documentation:
/**
* Calculates the product of price and quantity.
*
* @param price the price of one product
* @param quantity the number of products
* @return the total price
*/
static int calculateTotal(int price, int quantity) {
return price * quantity;
}
Javadoc tool এই comment থেকে documentation generate করতে পারে।
Good Comments
Good comment code-এর obvious behaviour repeat না করে useful context দেয়।
Poor:
// Set age to 30
int age = 30;
Comment code-ই repeat করছে।
Better context:
// The registration policy requires the learner to be at least 18.
int minimumRegistrationAge = 18;
আরও ভালো হতে পারে expressive code:
static final int MINIMUM_REGISTRATION_AGE = 18;
Explain Why, Not Only What
Poor:
// Add one to retry count
retryCount++;
Better:
// Count the failed attempt before deciding whether the account should be locked.
retryCount++;
Comment এমন information দিতে পারে যা code থেকে সরাসরি বোঝা যায় না।
Avoiding Too Many Comments
খারাপ naming বা structure cover করার জন্য comment ব্যবহার করা উচিত নয়।
Poor:
int x = 4990; // Course price
Better:
int coursePrice = 4990;
Meaningful code অনেক ক্ষেত্রে comment-এর প্রয়োজন কমায়।
Code Formatting
Formatting code-এর visual structure consistent রাখে।
Formatted code:
public class Main {
public static void main(String[] args) {
String courseName = "Java Foundation";
int durationInWeeks = 8;
System.out.println(courseName);
System.out.println(durationInWeeks);
}
}
Poorly formatted:
public class Main{public static void main(String[]args){String courseName="Java Foundation";int durationInWeeks=8;System.out.println(courseName);System.out.println(durationInWeeks);}}
দুটি code একই output দিতে পারে, কিন্তু প্রথমটি maintain করা সহজ।
IntelliJ IDEA Code Formatting
IntelliJ IDEA automatically code format করতে পারে।
Common shortcut:
Ctrl + Alt + L
macOS keymap অনুযায়ী shortcut হতে পারে:
Option + Command + L
IDE menu থেকেও format করা যায়:
Code
→ Reformat Code
Exact shortcut keymap অনুযায়ী ভিন্ন হতে পারে।
Before Formatting
public class Main{
public static void main(String[] args){
System.out.println("Hello");
}
}
After Formatting
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Code Style Consistency
একটি project-এ consistent style থাকা গুরুত্বপূর্ণ।
Avoid mixing:
int student_age;
int coursePrice;
int TotalMarks;
Preferred:
int studentAge;
int coursePrice;
int totalMarks;
Class naming-ও consistent:
Student
Course
GradeCalculator
One Statement Per Line
Recommended:
int firstNumber = 10;
int secondNumber = 20;
int total = firstNumber + secondNumber;
Avoid:
int firstNumber = 10; int secondNumber = 20; int total = firstNumber + secondNumber;
Declare Variables Near Their Use
Poor:
int total;
// Many unrelated statements
total = price * quantity;
System.out.println(total);
Better:
int total = price * quantity;
System.out.println(total);
Variable-related concept future lesson-এ বিস্তারিত শেখানো হবে।
Avoid Magic Values
Magic value হলো unexplained literal value।
Example:
if (age >= 18) {
System.out.println("Eligible");
}
Small example-এ এটি understandable।
Larger application-এ better:
static final int MINIMUM_VOTING_AGE = 18;
Then:
if (age >= MINIMUM_VOTING_AGE) {
System.out.println("Eligible");
}
Name value-এর meaning প্রকাশ করে।
Avoid Overly Long Names
Meaningful নাম প্রয়োজন, কিন্তু অতিরিক্ত দীর্ঘ নাম code unreadable করতে পারে।
Too long:
int totalNumberOfStudentsWhoAreCurrentlyEnrolledInTheJavaFoundationCourse;
Better:
int enrolledStudentCount;
Name concise এবং meaningful হওয়া উচিত।
Avoid Single-Letter Names
Avoid:
int a = 10;
int b = 20;
int c = a + b;
Better:
int firstNumber = 10;
int secondNumber = 20;
int total = firstNumber + secondNumber;
তবে loop counter-এর মতো ছোট scope-এ common name ব্যবহার করা হয়:
for (int i = 0; i < 10; i++) {
}
Loop lesson-এ এটি শেখানো হবে।
Common Syntax Errors
Missing Semicolon
Wrong:
int age = 30
Correct:
int age = 30;
Missing Quote
Wrong:
String name = "Sakib;
Correct:
String name = "Sakib";
Wrong Quote Type
Wrong:
String language = 'Java';
Correct:
String language = "Java";
Missing Parenthesis
Wrong:
System.out.println("Hello";
Correct:
System.out.println("Hello");
Missing Brace
Wrong:
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
Correct:
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Invalid Identifier
Wrong:
int 1stMark = 80;
Correct:
int firstMark = 80;
Keyword as Identifier
Wrong:
int class = 10;
Correct:
int classCount = 10;
Wrong Capitalization
Wrong:
system.out.println("Hello");
Correct:
System.out.println("Hello");
Space in Variable Name
Wrong:
String course name = "Java";
Correct:
String courseName = "Java";
Hyphen in Identifier
Wrong:
int course-price = 4990;
Correct:
int coursePrice = 4990;
Extra Comma
Wrong:
int total = 10 + 20,;
Correct:
int total = 10 + 20;
Reading a Compiler Error
Code:
public class Main {
public static void main(String[] args) {
int student age = 20;
}
}
Compiler error দেখাতে পারে:
';' expected
Actual problem variable name-এর মধ্যে space।
Correct:
int studentAge = 20;
Compiler error সবসময় human-friendly exact explanation নাও দিতে পারে।
Code structure এবং syntax rule বুঝে error interpret করতে হয়।
Syntax Error May Cause Multiple Errors
একটি ছোট syntax error compiler-এ অনেক error তৈরি করতে পারে।
Example:
String courseName = "Java Foundation;
int duration = 8;
System.out.println(courseName);
প্রথম line-এ closing quote missing।
Compiler পরবর্তী lineগুলোতেও error report করতে পারে।
Debugging approach:
- প্রথম error দেখুন
- সেই line এবং previous line check করুন
- Error fix করুন
- আবার compile করুন
- Remaining errors দেখুন
A Well-Formatted Java Program
public class CourseInformation {
static final int COURSE_DURATION_IN_WEEKS = 8;
public static void main(String[] args) {
String courseName = "Java and OOP Foundation";
String courseLanguage = "Bangla";
boolean isBeginnerFriendly = true;
System.out.println("Course: " + courseName);
System.out.println("Language: " + courseLanguage);
System.out.println("Duration: " + COURSE_DURATION_IN_WEEKS + " weeks");
System.out.println("Beginner friendly: " + isBeginnerFriendly);
}
}
এই program-এ naming conventions:
- Class:
CourseInformation - Constant:
COURSE_DURATION_IN_WEEKS - Variables:
courseName,courseLanguage,isBeginnerFriendly - Method:
main
Poorly Named Version
public class course_info {
static final int x = 8;
public static void main(String[] a) {
String c = "Java and OOP Foundation";
String l = "Bangla";
boolean b = true;
System.out.println(c);
System.out.println(l);
System.out.println(x);
System.out.println(b);
}
}
Code compile করতে পারে, কিন্তু meaning এবং convention দুর্বল।
Better naming code maintainability improve করে।
Before and After Refactoring Names
Before
int a = 80;
int b = 90;
int c = 85;
int d = a + b + c;
After
int mathematicsMark = 80;
int englishMark = 90;
int scienceMark = 85;
int totalMarks =
mathematicsMark
+ englishMark
+ scienceMark;
দ্বিতীয় version দেখে calculation-এর meaning বোঝা যায়।
Common Coding Convention Principles
Be Consistent
একই project-এ একই naming এবং formatting style follow করুন।
Prefer Clarity
Short code-এর চেয়ে understandable code বেশি গুরুত্বপূর্ণ।
Use Meaningful Names
Variable বা method কী represent করে, তা name থেকে বোঝা উচিত।
Keep Formatting Clean
Indentation এবং blank line logical structure দেখাবে।
Avoid Unnecessary Comments
Code নিজে explanatory করার চেষ্টা করুন।
Follow Team Standards
Professional project-এর নিজস্ব code style থাকতে পারে।
Use Automated Formatter
Manual inconsistency কমাতে IDE formatter ব্যবহার করুন।
Important Terms
Syntax
Programming language-এর grammatical rule।
Syntax Error
Language rule ভাঙার কারণে compilation error।
Keyword
Java-এর reserved word, যার predefined meaning আছে।
Identifier
Class, method, variable বা অন্য element-এর programmer-defined name।
Naming Convention
Name লেখার agreed style।
PascalCase
প্রতিটি word capital letter দিয়ে শুরু হয়।
Example:
GradeCalculator
camelCase
প্রথম word lowercase এবং পরবর্তী word capital letter দিয়ে শুরু হয়।
Example:
studentName
UPPER_SNAKE_CASE
Uppercase word underscore দিয়ে আলাদা করা হয়।
Example:
MAXIMUM_ATTEMPTS
Semicolon
Simple statement-এর ending নির্দেশ করে।
Code Block
Curly braces-এর মধ্যে related code।
Whitespace
Space, tab এবং new line।
Indentation
Nested code visually align করার spacing।
Comment
Developer-এর জন্য explanatory text, যা compiler execute করে না।
Javadoc
Java source code থেকে documentation তৈরির comment format।
Code Formatting
Code-এর spacing, indentation এবং layout consistent করা।
Practice Exercise 1: Valid or Invalid Identifiers
নিচের identifierগুলো valid নাকি invalid, তা লিখুন।
studentName2ndStudentcourse_priceclass_total$generatedValuestudent nametotalMarkscourse-priceMAX_SIZE
Valid হলেও conventionally poor হলে সেটিও উল্লেখ করুন।
Practice Exercise 2: Fix the Identifier Names
নিচের poor identifierগুলো Java convention অনুযায়ী improve করুন:
student_name
Studentage
COURSEname
total-price
n
isactive
Possible meaning:
- Student name
- Student age
- Course name
- Total price
- Number of students
- Active status
Practice Exercise 3: Name Each Element
নিচের requirement অনুযায়ী appropriate name লিখুন:
- Student class
- Bank account class
- Course price variable
- Student count variable
- Calculate average method
- Display result method
- Minimum age constant
- Maximum login attempts constant
- User active boolean
- Payment completed boolean
Practice Exercise 4: Fix the Syntax Errors
নিচের code fix করুন:
public class main {
public static void main(String[] args) {
String course name = 'Java Foundation'
int 1duration = 8
system.out.println(course name)
}
}
Expected output:
Java Foundation
Practice Exercise 5: Format the Code
নিচের code readableভাবে format করুন:
public class Main{public static void main(String[]args){String courseName="Java Foundation";int duration=8;System.out.println(courseName);System.out.println(duration);}}
Practice Exercise 6: Improve the Names
নিচের code-এর variable name improve করুন:
int a = 500;
int b = 3;
int c = a * b;
System.out.println(c);
ধরা যাক:
aproduct pricebquantityctotal price
Practice Exercise 7: Class Naming
নিচের names Java class naming convention অনুযায়ী correct করুন:
student
bank_account
gradecalculator
course-enrollment
paymentservice
Practice Exercise 8: Constant Naming
নিচের constant names UPPER_SNAKE_CASE-এ লিখুন:
minimumVotingAge
maximumLoginAttempts
defaultPageSize
taxRate
courseDurationInWeeks
Practice Exercise 9: Comments
নিচের program-এ:
- একটি single-line comment
- একটি multi-line comment
- একটি Javadoc comment
যোগ করুন।
public class GradeCalculator {
public static void main(String[] args) {
int firstMark = 80;
int secondMark = 90;
int totalMarks = firstMark + secondMark;
System.out.println(totalMarks);
}
}
Practice Exercise 10: Remove Unnecessary Comments
নিচের code improve করুন:
public class Main {
public static void main(String[] args) {
// Create an integer called a
int a = 18;
// Print a
System.out.println(a);
}
}
Variable-এর meaningful name দিন এবং unnecessary comment remove করুন।
Practice Exercise 11: Match the Convention
নিচের item এবং naming style match করুন:
Items:
- Class
- Method
- Variable
- Constant
- Package
Styles:
PascalCase
camelCase
UPPER_SNAKE_CASE
lowercase
Practice Exercise 12: Predict Compilation
প্রতিটি code compile করবে কি না লিখুন।
Example 1
int studentAge = 20;
Example 2
int student age = 20;
Example 3
String class = "Java";
Example 4
String course2 = "Java";
Example 5
double product-price = 50.0;
Example 6
boolean isAvailable = true;
Practice Exercise 13: Identify Keywords and Identifiers
নিচের code থেকে সব keyword এবং identifier আলাদা করে লিখুন:
public class CourseApplication {
public static void main(String[] args) {
int durationInWeeks = 8;
boolean isAvailable = true;
System.out.println(durationInWeeks);
System.out.println(isAvailable);
}
}
Practice Exercise 14: Fix the Braces
নিচের code-এর braces এবং indentation ঠিক করুন:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Adult");
}
}
}
Practice Exercise 15: Build a Clean Course Information Program
একটি Java program লিখুন, যেখানে থাকবে:
- Class name:
CourseInformation - Constant:
COURSE_DURATION_IN_WEEKS - Variable:
courseName - Variable:
courseLanguage - Boolean variable:
isBeginnerFriendly
Expected output:
Course: Java and OOP Foundation
Language: Bangla
Duration: 8 weeks
Beginner friendly: true
সব naming convention এবং formatting rule follow করুন।
Knowledge Check
Question 1
Java syntax কী?
Question 2
Syntax error কখন ঘটে?
Question 3
Java case-sensitive বলতে কী বোঝায়?
Question 4
Keyword কী?
Question 5
Keyword কি variable name হিসেবে ব্যবহার করা যায়?
Question 6
Identifier কী?
Question 7
Identifier কি number দিয়ে শুরু করা যায়?
Question 8
Identifier-এর মধ্যে space ব্যবহার করা যায় কি?
Question 9
Java class-এর common naming convention কী?
Question 10
Variable এবং method-এর common naming convention কী?
Question 11
Constant-এর common naming convention কী?
Question 12
Package name সাধারণত কীভাবে লেখা হয়?
Question 13
Semicolon-এর কাজ কী?
Question 14
Curly braces কী define করে?
Question 15
Parentheses কোথায় ব্যবহার করা হয়?
Question 16
Double quotes এবং single quotes-এর পার্থক্য কী?
Question 17
Whitespace কী?
Question 18
Indentation compiler-এর জন্য বাধ্যতামূলক কি?
Question 19
Single-line comment কোন symbol দিয়ে শুরু হয়?
Question 20
Multi-line comment কীভাবে লেখা হয়?
Question 21
Javadoc comment কোন symbol দিয়ে শুরু হয়?
Question 22
Meaningful naming কেন গুরুত্বপূর্ণ?
Question 23
student_name valid Java identifier কি?
Question 24
student_name কি recommended Java variable convention?
Question 25
Compiler অনেক error দেখালে কোন error আগে fix করা উচিত?
Knowledge Check Answers
Answer 1
Java syntax হলো Java code লেখার grammatical rule এবং structure।
Answer 2
Code Java language-এর rule follow না করলে syntax error ঘটে।
Answer 3
Java uppercase এবং lowercase letter-কে আলাদা identifier হিসেবে বিবেচনা করে।
Answer 4
Keyword হলো Java language-এর reserved word, যার predefined meaning আছে।
Answer 5
না। Java keyword variable, method বা class name হিসেবে ব্যবহার করা যায় না।
Answer 6
Identifier হলো programmer-এর দেওয়া class, method, variable, parameter বা অন্য program element-এর নাম।
Answer 7
না। Identifier number দিয়ে শুরু করা যায় না।
Answer 8
না। Identifier-এর মধ্যে space ব্যবহার করা যায় না।
Answer 9
Class-এর common convention হলো:
PascalCase
Answer 10
Variable এবং method-এর common convention হলো:
camelCase
Answer 11
Constant-এর common convention হলো:
UPPER_SNAKE_CASE
Answer 12
Package name সাধারণত lowercase-এ লেখা হয়।
Answer 13
Semicolon simple statement-এর ending নির্দেশ করে।
Answer 14
Curly braces class, method, condition এবং loop-এর code block define করে।
Answer 15
Parentheses method declaration, method call, condition এবং expression grouping-এ ব্যবহার করা হয়।
Answer 16
Double quotes String value-এর জন্য এবং single quotes একটি char value-এর জন্য ব্যবহার করা হয়।
Answer 17
Whitespace হলো space, tab এবং new line।
Answer 18
Java compiler সাধারণত indentation require করে না, কিন্তু readable এবং maintainable code-এর জন্য indentation গুরুত্বপূর্ণ।
Answer 19
Single-line comment শুরু হয়:
//
Answer 20
Multi-line comment:
/*
* Comment
*/
Answer 21
Javadoc comment শুরু হয়:
/**
Answer 22
Meaningful name code-এর purpose, data এবং behaviour সহজে বুঝতে সাহায্য করে।
Answer 23
হ্যাঁ। student_name valid Java identifier।
Answer 24
না। Java variable-এর recommended convention হলো:
studentName
Answer 25
সাধারণত compiler-এর report করা প্রথম error আগে fix করা উচিত, কারণ একটি syntax error পরবর্তী অনেক error তৈরি করতে পারে।
Lesson Summary
এই lesson-এ আমরা শিখেছি:
- Syntax হলো Java language-এর grammatical rule
- Syntax rule ভাঙলে compilation error হয়
- Java case-sensitive
- Java keyword reserved এবং identifier হিসেবে ব্যবহার করা যায় না
- Identifier class, method, variable এবং parameter-এর নাম
- Identifier number দিয়ে শুরু করা যায় না
- Identifier-এর মধ্যে space এবং hyphen ব্যবহার করা যায় না
- Class name সাধারণত PascalCase follow করে
- Variable, method এবং parameter camelCase follow করে
- Constant UPPER_SNAKE_CASE follow করে
- Package name lowercase-এ লেখা হয়
- Meaningful name code readable এবং maintainable করে
- Simple statement সাধারণত semicolon দিয়ে শেষ হয়
- Curly braces code block define করে
- Parentheses method, condition এবং expression-এ ব্যবহার হয়
- Double quotes String এবং single quotes
char-এর জন্য - Whitespace এবং indentation code-এর structure readable করে
- Single-line, multi-line এবং Javadoc comment-এর উদ্দেশ্য আলাদা
- Comment code-এর obvious behaviour repeat না করে useful context দেওয়া উচিত
- Consistent formatting team collaboration এবং code review সহজ করে
- IntelliJ IDEA formatter ব্যবহার করে code automatically format করা যায়
- প্রথম compiler error fix করলে অনেক related error resolve হতে পারে
Next Lesson
পরবর্তী lesson-এ আমরা Java variable এবং constant সম্পর্কে বিস্তারিত শিখব।
আমরা জানব:
- Variable কী
- Variable declaration এবং initialization
- Assignment
- Variable-এর value পরিবর্তন করা
- Local variable
- Variable scope-এর প্রাথমিক ধারণা
- Meaningful variable name
finalkeyword- Constant
- Multiple variable declaration
- Common variable-related error