☕ Java (สอนแบบครบระดับ)

Java เป็นภาษา OOP ที่เสถียร ใช้ในระบบองค์กร, Android, และ backend ขนาดใหญ่ พร้อม ecosystem ที่แข็งแรง

🧭 Java คืออะไร

ทำไมต้องเรียน Java?

  • Enterprise: ใช้ในระบบ banking, insurance
  • Android: พัฒนา mobile apps
  • Backend: Spring Boot สำหรับ API
  • Portable: Write Once, Run Anywhere (JVM)

Hello World

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

⚙️ JDK + IDE

ติดตั้ง JDK

# ตรวจสอบ version
java -version
javac -version

# Compile และ Run
javac Main.java    # สร้าง Main.class
java Main          # รันโปรแกรม

IDE แนะนำ

IntelliJ IDEA

แนะนำสำหรับ professional

VS Code

+ Extension Pack for Java

🔤 ตัวแปรและชนิดข้อมูล

Primitive Types

// Integer types
byte b = 127;           // 8-bit
short s = 32767;        // 16-bit
int i = 100;            // 32-bit (ใช้บ่อย)
long l = 100000L;       // 64-bit

// Floating point
float f = 3.14f;        // 32-bit
double d = 3.14159;     // 64-bit (ใช้บ่อย)

// Others
boolean active = true;  // true/false
char c = 'A';           // single character

Reference Types

// String
String name = "Somchai";
String greeting = "Hello, " + name;

// Array
int[] numbers = {1, 2, 3, 4, 5};
String[] names = new String[3];
names[0] = "John";

// var (Java 10+)
var score = 100;        // inferred as int
var message = "Hello";  // inferred as String

🧮 เงื่อนไขและลูป

If-Else & Switch

int score = 75;

if (score >= 80) {
    System.out.println("A");
} else if (score >= 70) {
    System.out.println("B");
} else {
    System.out.println("C");
}

// Switch (Java 14+)
String grade = switch (score / 10) {
    case 10, 9, 8 -> "A";
    case 7 -> "B";
    default -> "C";
};

Loops

// for loop
for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

// enhanced for (foreach)
int[] nums = {1, 2, 3};
for (int n : nums) {
    System.out.println(n);
}

// while
int count = 0;
while (count < 5) {
    System.out.println(count++);
}

🏛️ Class เบื้องต้น

Class & Object

public class Student {
    // Fields
    private String name;
    private int age;
    
    // Constructor
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // Getter/Setter
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    
    // Method
    public void introduce() {
        System.out.println("Hi, I'm " + name);
    }
}

// Usage
Student s = new Student("Somchai", 20);
s.introduce();

Methods

public class Calculator {
    // Method with return
    public int add(int a, int b) {
        return a + b;
    }
    
    // Overloading
    public double add(double a, double b) {
        return a + b;
    }
    
    // Static method
    public static int multiply(int a, int b) {
        return a * b;
    }
}

// Usage
Calculator calc = new Calculator();
calc.add(2, 3);
Calculator.multiply(2, 3);  // static

🏁 สรุป

  • ✅ Java ใช้ในงาน Enterprise และ Android
  • ✅ Primitive types: int, double, boolean
  • ✅ Class = blueprint, Object = instance
  • ✅ Method overloading รองรับหลาย signature

📝 แบบฝึกหัด

Variables & Control

  1. เขียนโปรแกรมรับคะแนนแล้วตัดเกรด
  2. วนลูปพิมพ์เลข 1-10

Class

  1. สร้างคลาส Book (title, author, price)
  2. สร้าง Calculator class พร้อม add, subtract