Lesson 3 of 24 First Java Program
Lesson 3 12 min read

Your First Java Program

အခု lesson မှာ Java program တစ်ခု လက်တွေ့ ရေးကြည့်မှာ ဖြစ်ပါတယ်။ Program structure, syntax rules, common mistakes တွေကို အသေးစိတ် လေ့လာကြမယ်။

1. The Classic "Hello, World!" Program

Let's start with the traditional first program that every programmer writes:

Java - HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Output
Hello, World!
Program အလုပ်လုပ်ပုံ

ဒီ program က screen မှာ "Hello, World!" ဆိုတဲ့ text ကို ပြပေးမှာ ဖြစ်ပါတယ်။ ရိုးရှင်းပေမယ့် Java programming ရဲ့ အခြေခံ structure အားလုံး ပါဝင်ပါတယ်။

How it works:

  1. JVM က program ကို start လုပ်တဲ့ အခါ main method ကို ရှာပါတယ်
  2. main method အထဲက statements တွေကို top-to-bottom execute လုပ်ပါတယ်
  3. System.out.println() က text ကို console မှာ print လုပ်ပေးပါတယ်
  4. Program က ပြီးဆုံးပါတယ် (no more statements to execute)

2. Java Program Structure (အသေးစိတ် ခွဲခြမ်းစိတ်ဖြာခြင်း)

Annotated Structure
// 1. Class Declaration
public class HelloWorld {
    
    // 2. Main Method (Entry Point)
    public static void main(String[] args) {
        
        // 3. Method Body (Executable Code)
        System.out.println("Hello, World!");
        
    } // End of main method
    
} // End of class

Part 1: Class Declaration

Java
public class HelloWorld {
    // class body
}
Class Declaration အကြောင်း

public - Access modifier ပါ။ ဒီ class ကို ဘယ်ကနေမဆို access လုပ်လို့ ရတယ် ဆိုတဲ့ သဘောပါ။

  • public = Everyone can access
  • private = Only within this class
  • protected = Subclasses can access
  • No modifier = Package-private (same package only)

class - Java keyword ပါ။ Class တစ်ခု declare လုပ်ရန် သုံးပါတယ်။ Java မှာ code အားလုံး class ထဲမှာ ရေးရပါတယ်။

HelloWorld - Class ရဲ့ နာမည်ပါ။ Naming Rules:

  • CapitalCase (PascalCase) သုံးပါ - စာလုံးကြီးနဲ့ စပါ (HelloWorld, MyClass, StudentInfo)
  • File name နဲ့ exactly တူရပါမယ် - HelloWorld class ဆို HelloWorld.java ဖြစ်ရမယ်
  • Letters, digits, underscore, dollar sign သာ သုံးလို့ ရပါတယ်
  • Digit နဲ့ မစနိုင်ပါ - 3Class ❌, Class3 ✅
  • Java keywords မသုံးရပါ - class, public, int စတာတွေ ❌
  • Case-sensitive ဖြစ်ပါတယ် - HelloWorld ≠ helloworld

{ } - Curly braces က class body ကို define လုပ်ပါတယ်။ Class ရဲ့ code အားလုံး curly braces အတွင်းမှာ ရေးရပါတယ်။

Part 2: Main Method

Java
public static void main(String[] args) {
    // method body
}
Main Method - Entry Point

Main method က Java program ရဲ့ entry point (စတင်ရာ နေရာ) ဖြစ်ပါတယ်။ JVM က program run တဲ့ အခါ main method ကို အရင်ဆုံး ရှာပြီး execute လုပ်ပါတယ်။

Signature breakdown (အစိတ်အပိုင်းတစ်ခုချင်းစီ):

public - JVM က ဘယ်ကနေမဆို access လုပ်နိုင်ဖို့ public ဖြစ်ရပါမယ်။ JVM က class အပြင်ကနေ ခေါ်မှာ ဖြစ်လို့ပါ။

static - Object မလုပ်ဘဲ ခေါ်လို့ ရဖို့ static ဖြစ်ရပါမယ်။

  • JVM က HelloWorld object မလုပ်ဘဲ HelloWorld.main() ကို တိုက်ရိုက် ခေါ်ချင်လို့ပါ
  • static methods တွေက class-level ဖြစ်ပြီး object မလိုပါ
  • Non-static ဆို object create လုပ်ရမှာ ဖြစ်ပါတယ် - HelloWorld obj = new HelloWorld();

void - Return type ပါ။ void ဆိုတာ ဘာမှ return မလုပ်ဘူး ဆိုတဲ့ သဘောပါ။

  • void = Returns nothing
  • int = Returns integer number
  • String = Returns text
  • Main method က void ဖြစ်ရပါမယ် (JVM requirement)

main - Method နာမည်ပါ။ ဒါက fixed name ဖြစ်ပြီး ပြောင်းလို့ မရပါ။ JVM က "main" ဆိုတဲ့ နာမည်ကို သတ်မှတ်ထားလို့ပါ။

String[] args - Parameter ပါ။ Command-line arguments တွေကို လက်ခံဖို့ ဖြစ်ပါတယ်။

  • String[] = Array of strings (text တွေ စုစည်းထားတာ)
  • args = Parameter name (ကိုယ်ကြိုက်တာ ပေးလို့ရပေမယ့် "args" က standard)
  • Command line ကနေ: java HelloWorld arg1 arg2 arg3
  • args[0] = "arg1", args[1] = "arg2", args[2] = "arg3"

📌 Main method signature က exact ဖြစ်ရပါမယ်:

  • ✅ public static void main(String[] args)
  • ✅ public static void main(String args[]) - Also valid
  • ✅ public static void main(String... args) - Varargs also valid
  • ❌ static public void main(String[] args) - Order မှားတယ် (works but not standard)
  • ❌ public void main(String[] args) - static မရှိတော့ error
  • ❌ public static int main(String[] args) - void မဟုတ်ရင် error

Part 3: Print Statement

Java
System.out.println("Hello, World!");
Print Statement အသေးစိတ်

System - java.lang package ထဲက built-in class တစ်ခု ဖြစ်ပါတယ်။ System ကို import မလုပ်ရပါ၊ java.lang က automatic import ဖြစ်လို့ပါ။

out - System class ရဲ့ static field တစ်ခု ဖြစ်ပါတယ်။ ဒါက PrintStream object တစ်ခု ဖြစ်ပြီး standard output stream (console) ကို represent လုပ်ပါတယ်။

println() - "Print Line" ရဲ့ အတိုကောက်ပါ။ Text ကို print လုပ်ပြီး cursor ကို next line ကို ရွှေ့ပေးပါတယ်။

  • println() = Print + New line
  • print() = Print only (no new line)
  • printf() = Formatted print (like C language)

"Hello, World!" - String literal ပါ။ Print လုပ်ချင်တဲ့ text ကို double quotes (" ") ထဲမှာ ထည့်ပါတယ်။

  • Double quotes လိုပါတယ် - Single quotes (') က character အတွက်သာ
  • Case-sensitive ဖြစ်ပါတယ် - "Hello" ≠ "hello"
  • Special characters သုံးလို့ ရပါတယ် - \n (newline), \t (tab), \" (quote)

; - Semicolon က Java မှာ statement ရဲ့ အဆုံး marker ပါ။ မဖြစ်မနေ ထည့်ရပါမယ်။ Semicolon မပါရင် compilation error ပေါ်ပါတယ်။

3. How to Write, Compile, and Run (အဆင့်ဆင့် လုပ်နည်း)

Step 1: Write the Code

Create a file named HelloWorld.java using any text editor (Notepad, VS Code, IntelliJ IDEA):

📝 File Naming Rules

  • File name MUST match class name exactly - Case-sensitive!
  • File extension MUST be .java
  • If class name is HelloWorld, file must be HelloWorld.java
  • ❌ helloworld.java - Wrong case
  • ❌ Hello.java - Wrong name
  • ❌ HelloWorld.txt - Wrong extension
  • ✅ HelloWorld.java - Correct!

Step 2: Compile the Program

Open Command Prompt/Terminal in the folder containing your file:

Command Line
javac HelloWorld.java
Compilation အကြောင်း

javac = Java Compiler command

What happens during compilation:

  1. Compiler က HelloWorld.java source file ကို ဖတ်ပါတယ်
  2. Syntax errors တွေ စစ်ဆေးပါတယ် (typos, missing semicolons, etc.)
  3. အားလုံး မှန်ကန်ရင် HelloWorld.class bytecode file ကို create လုပ်ပါတယ်
  4. Success ဖြစ်ရင် output message မရှိပါ (silent success)
  5. Error ရှိရင် error messages ပြပါတယ်

After compilation: HelloWorld.class file ပေါ်လာပါမယ်။ ဒါက bytecode file ဖြစ်ပြီး JVM က execute လုပ်မှာ ဖြစ်ပါတယ်။

Step 3: Run the Program

Command Line
java HelloWorld
Output
Hello, World!
Execution အကြောင်း

java = Java Runtime command (JVM launcher)

⚠️ အရေးကြီးတဲ့ Points:

  • Class name သာ သုံးပါ, file extension (.class) မပါဘူး
  • ✅ java HelloWorld - Correct
  • ❌ java HelloWorld.class - Wrong! Don't include .class
  • ❌ java HelloWorld.java - Wrong! Don't include .java

What happens during execution:

  1. JVM က HelloWorld.class file ကို load လုပ်ပါတယ်
  2. main method ကို ရှာပြီး execution start လုပ်ပါတယ်
  3. Method body ထဲက statements တွေကို top-to-bottom execute လုပ်ပါတယ်
  4. System.out.println() က console မှာ text print လုပ်ပါတယ်
  5. No more statements ရှိတော့ program terminate ဖြစ်ပါတယ်

4. More Print Examples (နောက်ထပ် ဥပမာများ)

Example 1: Multiple Print Statements

Java - Welcome.java
public class Welcome {
    public static void main(String[] args) {
        System.out.println("Welcome to Java Programming!");
        System.out.println("Java is powerful and versatile.");
        System.out.println("Let's learn Java together!");
    }
}
Output
Welcome to Java Programming!
Java is powerful and versatile.
Let's learn Java together!
ဘာကြောင့် သုံးသလဲ?

Multiple println() statements တွေ သုံးရင် line တစ်ကြောင်းချင်းစီ print လုပ်ပါတယ်။ Statement တစ်ခုချင်းစီ execute ပြီးရင် cursor က next line ကို ရွှေ့ပါတယ်။

Statements တွေက top-to-bottom order အတိုင်း execute ဖြစ်ပါတယ်။

Example 2: print() vs println()

Java - PrintDemo.java
public class PrintDemo {
    public static void main(String[] args) {
        // Using print() - stays on same line
        System.out.print("Java ");
        System.out.print("is ");
        System.out.print("fun!");
        
        // Move to next line
        System.out.println();
        
        // Using println() - moves to next line
        System.out.println("Learning");
        System.out.println("Programming");
    }
}
Output
Java is fun!
Learning
Programming
ကွာခြားချက်

print() - Text print လုပ်ပြီး cursor က same line မှာပဲ ရှိနေပါတယ်

  • တစ်ကြောင်းတည်းမှာ စာများများ ရေးချင်ရင် သုံးပါ
  • Example: Building sentences piece by piece

println() - Text print လုပ်ပြီး cursor က next line ကို ရွှေ့ပါတယ်

  • Line သစ် တစ်ကြောင်းချင်းစီ print ချင်ရင် သုံးပါ
  • Most commonly used for output

println() with no argument - Empty line တစ်ကြောင်း ထည့်ပေးပါတယ် (line break only)

Example 3: Printing Numbers and Expressions

Java - Numbers.java
public class Numbers {
    public static void main(String[] args) {
        // Printing numbers
        System.out.println(100);
        System.out.println(3.14);
        
        // Printing expressions (calculations)
        System.out.println(10 + 20);
        System.out.println(50 - 15);
        System.out.println(5 * 6);
        System.out.println(100 / 4);
        
        // Mixing text and numbers
        System.out.println("Result: " + (10 + 5));
        System.out.println("Pi value: " + 3.14159);
    }
}
Output
100
3.14
30
35
30
25
Result: 15
Pi value: 3.14159
အရေးကြီးတဲ့ အချက်များ

Numbers without quotes: Quotes မပါရင် number အဖြစ် treat လုပ်ပြီး calculations လုပ်လို့ ရပါတယ်

  • System.out.println(10 + 20); → Output: 30 (calculation)
  • System.out.println("10 + 20"); → Output: 10 + 20 (text)

Concatenation with + operator: Text နဲ့ numbers တွေကို + operator နဲ့ ပေါင်းလို့ ရပါတယ်

  • "Result: " + 15 → Result: 15
  • Text ပါရင် အားလုံး string အဖြစ် convert ဖြစ်ပါတယ်

Parentheses for calculations: Calculation ကို အရင် လုပ်ချင်ရင် parentheses () သုံးပါ

  • "Result: " + (10 + 5) → Calculate first, then concat → Result: 15
  • "Result: " + 10 + 5 → Concat left-to-right → Result: 105 (string concat!)

Example 4: Special Characters (Escape Sequences)

Java - SpecialChars.java
public class SpecialChars {
    public static void main(String[] args) {
        // New line
        System.out.println("Line 1\nLine 2\nLine 3");
        
        // Tab
        System.out.println("Name:\tJohn");
        System.out.println("Age:\t25");
        
        // Quotes
        System.out.println("She said, \"Hello!\"");
        
        // Backslash
        System.out.println("File path: C:\\Users\\Documents\\file.txt");
        
        // Single quote (in char context)
        System.out.println("It\'s a beautiful day!");
    }
}
Output
Line 1
Line 2
Line 3
Name:	John
Age:	25
She said, "Hello!"
File path: C:\Users\Documents\file.txt
It's a beautiful day!
Escape Sequences (ထူးခြားတဲ့ characters)

Escape sequences က backslash \ နဲ့ စတဲ့ special character combinations ပါ:

  • \n - New line (line break)
  • \t - Tab (horizontal spacing)
  • \" - Double quote (inside string)
  • \' - Single quote
  • \\ - Backslash (literal backslash)
  • \r - Carriage return
  • \b - Backspace

ဘာကြောင့် လိုအပ်လဲ? Special meaning ရှိတဲ့ characters တွေကို string ထဲမှာ သုံးချင်ရင် escape sequences သုံးရပါတယ်။ ဥပမာ - quotes ကို string ထဲမှာ ပြချင်ရင် \" သုံးရပါတယ်။

5. Common Mistakes and How to Fix Them (အမှားများနှင့် အဖြေများ)

Mistake 1: File Name Doesn't Match Class Name

❌ Wrong - Hello.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello!");
    }
}
Error
error: class HelloWorld is public, should be declared in a file named HelloWorld.java
✅ Correct - HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello!");
    }
}
Solution

File name နဲ့ public class name က exactly တူရပါမယ် - case-sensitive!

Mistake 2: Missing Semicolon

❌ Wrong
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello")  // Missing semicolon!
    }
}
Error
error: ';' expected
✅ Correct
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello");  // Semicolon added
    }
}

Mistake 3: Missing or Mismatched Braces

❌ Wrong
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello");
    // Missing closing brace for main method!
}
Error
error: reached end of file while parsing
}
✅ Correct
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello");
    }  // Closing brace for main
}  // Closing brace for class

Mistake 4: Wrong main Method Signature

❌ Wrong - Missing static
public class Test {
    public void main(String[] args) {  // Missing 'static'
        System.out.println("Hello");
    }
}
Error at Runtime
Error: Main method is not static in class Test
✅ Correct
public class Test {
    public static void main(String[] args) {  // 'static' included
        System.out.println("Hello");
    }
}

Mistake 5: Case Sensitivity Errors

❌ Wrong
public class Test {
    public static void main(String[] args) {
        system.out.println("Hello");  // 'system' should be 'System'
    }
}
Error
error: cannot find symbol
symbol: variable system

⚠️ Java Case Sensitivity Rules

  • System ≠ system
  • String ≠ string
  • main ≠ Main
  • println ≠ Println
  • Class names, method names, keywords - အားလုံး case-sensitive ဖြစ်ပါတယ်

Mistake 6: Forgetting Double Quotes

❌ Wrong
System.out.println(Hello);  // Missing quotes!
Error
error: cannot find symbol
symbol: variable Hello
✅ Correct
System.out.println("Hello");  // Quotes added

6. Key Takeaways (မှတ်သားရမည့် အချက်များ)

  • Every Java program must have a class - All code lives inside classes
  • File name must match public class name exactly - Case-sensitive!
  • Main method is the entry point - Signature must be exact
  • Statements end with semicolons - Don't forget the ;
  • Curly braces must match - Every { needs a corresponding }
  • Java is case-sensitive - System ≠ system
  • Compile with javac - javac FileName.java
  • Run with java - java ClassName (no .class extension)
  • println() adds newline - print() doesn't
  • Use escape sequences for special characters - \n, \t, \", etc.

💡 Practice Exercise

Task: Write a program named MyInfo.java that displays your information:

Expected Output
Name: [Your Name]
Age: [Your Age]
Hobby: [Your Hobby]
Learning: Java Programming

Requirements:

  • Class name must be MyInfo
  • Use multiple println() statements
  • File name must be MyInfo.java
  • Don't forget semicolons!

💡 Quick Quiz

Q1: Which command compiles a Java program?

Q2: What is the output of: System.out.print("A"); System.out.print("B");

Q3: What does \n do in a string?

Previous Lesson
Next Lesson

Your Notes

0 notes
Saved