© Khmer Angkor Academy - sophearithput168

មូលដ្ឋាន Java

រចនាសម្ព័ន្ធកម្មវិធី Java

🏗️ រចនាសម្ព័ន្ធមូលដ្ឋាន

កម្មវិធី Java គ្រប់ទាំងអស់ត្រូវតែមាន class និង main method

public class HelloWorld {           // Class
    public static void main(String[] args) {  // Main method
        System.out.println("Hello!");  // Code
    }
}

🔍 ពន្យល់ម្តងមួយៗ:

1️⃣ public class HelloWorld

  • public: access modifier - អាចចូលប្រើបានពីគ្រប់ទីកន្លែង
  • class: keyword ដើម្បីបង្កើត class
  • HelloWorld: ឈ្មោះ class (ត្រូវតែចាប់ផ្តើមដោយអក្សរធំ)
  • { }: block code របស់ class

2️⃣ public static void main(String[] args)

នេះគឺជា main method - ចំណុចចាប់ផ្តើមកម្មវិធី។

  • public: អាចហៅបានពីខាងក្រៅ
  • static: អាចហៅដោយមិនចាំបាច់បង្កើត object
  • void: មិនមាន return value
  • main: ឈ្មោះ method (JVM ស្វែងរក main ដើម្បីចាប់ផ្តើម)
  • String[] args: parameter សម្រាប់ទទួល command-line arguments

3️⃣ System.out.println()

  • System: built-in class
  • out: output stream object
  • println(): method បោះពុម្ពហើយបន្ទាត់ថ្មី

📋 វាក្យសម្ព័ន្ធមូលដ្ឋាន

វាក្យសម្ព័ន្ធ ការពន្យល់
public class Name { } បង្កើត class
public static void main() { } Main method
System.out.println(); បោះពុម្ពជាមួយបន្ទាត់ថ្មី
System.out.print(); បោះពុម្ពគ្មានបន្ទាត់ថ្មី
// comment Single-line comment

❓ ហេតុអ្វីត្រូវការ class?

Java គឺជា Object-Oriented Programming language។ គ្រប់យ៉ាងគឺជា object ហើយ object មកពី class។

ឧទាហរណ៍ក្នុងជីវិតពិត:

  • Class Car: blueprint (ផែនការ) របស់រថយន្ត
  • Object myCar: រថយន្តពិតប្រាកដមួយដែលបង្កើតពី blueprint

⚠️ ច្បាប់សំខាន់ៗ:

  1. ឈ្មោះឯកសារ = ឈ្មោះ class: HelloWorld.java → class HelloWorld
  2. Case-sensitive: HelloWorld ≠ helloworld
  3. ត្រូវមាន main method: ដើម្បី run កម្មវិធី
  4. មួយឯកសារ មួយ public class: អាចមាន class ច្រើន ប៉ុន្តែ public មួយ
Java Code
Click "Run" to execute the Java code

📐 Syntax Rules (ច្បាប់វាក្យសម្ព័ន្ធ)

🔤 ច្បាប់ការសរសេរកូដ Java

1. Semicolon (;)

រាល់ statement ត្រូវបញ្ចប់ដោយ ;

System.out.println("Hello");  // ✅ ត្រឹមត្រូវ
System.out.println("Hello")   // ❌ កំហុស - គ្មាន ;

2. Curly Braces { }

Block code ត្រូវនៅក្នុង { }

public class Test {
    public static void main(String[] args) {
        if (true) {
            System.out.println("Yes");
        }
    }
}

3. Case Sensitivity

Java ប្រកាន់អក្សរធំតូច។

int number = 10;  // ✅
int Number = 20;  // ✅ - ខុសគ្នា!
int NUMBER = 30;  // ✅ - ខុសគ្នា!

4. Naming Conventions

  • Classes: PascalCase (HelloWorld, MyClass)
  • Methods/Variables: camelCase (myMethod, firstName)
  • Constants: UPPER_CASE (MAX_VALUE, PI)

📏 Code Formatting

// ✅ ល្អ - មានការកំណត់រចនាសម្ព័ន្ធ
public class MyClass {
    public static void main(String[] args) {
        int x = 10;
        if (x > 5) {
            System.out.println("Greater");
        }
    }
}

// ❌ មិនល្អ - លំបាកអាន
public class MyClass{public static void main(String[] args){
int x=10;if(x>5){System.out.println("Greater");}}}
Java Code
Click "Run" to execute the Java code

💬 Comments និង Output

📝 Comments (ការអធិប្បាយ)

Comments ប្រើដើម្បីពន្យល់កូដ។ JVM មិនអាន comments។

1. Single-line Comment

// នេះជា comment
int age = 25;  // អាយុ

2. Multi-line Comment

/*
   នេះជា comment ច្រើនបន្ទាត់
   អាចសរសេរបានច្រើនបន្ទាត់
*/
int score = 100;

3. Documentation Comment (Javadoc)

/**
 * Method នេះគណនាផលបូក
 * @param a លេខទី 1
 * @param b លេខទី 2
 * @return ផលបូក
 */
public int add(int a, int b) {
    return a + b;
}

💡 ប្រើ Comments នៅពេលណា?

  • ពន្យល់ logic ស្មុគស្មាញ: ធ្វើឱ្យអ្នកដទៃយល់
  • TODO/FIXME: សម្គាល់កន្លែងត្រូវកែ
  • Documentation: ពន្យល់ method, class
  • Debug: disable code បណ្តោះអាសន្ន

🖨️ Output Methods

Method ពន្យល់ ឧទាហរណ៍
println() បោះពុម្ព + បន្ទាត់ថ្មី Hello
World
print() បោះពុម្ពគ្មានបន្ទាត់ថ្មី HelloWorld
printf() បោះពុម្ពតាម format Name: John

🎨 printf() Formatting

String name = "John";
int age = 25;
double price = 99.99;

// Format specifiers:
System.out.printf("Name: %s%n", name);      // %s = String
System.out.printf("Age: %d%n", age);        // %d = Integer
System.out.printf("Price: %.2f%n", price);  // %.2f = Float (2 decimal)

🔗 String Concatenation

String firstName = "John";
String lastName = "Doe";
int age = 25;

// ប្រើ +
System.out.println("Name: " + firstName + " " + lastName);
System.out.println("Age: " + age);

// ប្រើ printf
System.out.printf("Name: %s %s, Age: %d%n", firstName, lastName, age);
Java Code
Click "Run" to execute the Java code