Java basics: classes, main, and output · Java basics: classes, main, และ output
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
AP Computer Science A uses Java
- The AP CSA exam is written in Java.
- Java is strict: every program lives inside a class, and starts from a method called main.
- You already think like a programmer — here we learn Java's rules.
ไทย
AP Computer Science A ใช้ Java
- การสอบ AP CSA เขียนในภาษา Java
- Java เป็น strict: ทุกโปรแกรมอยู่ภายใน class, และเริ่มจาก method ที่เรียกว่า main
- คุณคิดเหมือนนักพัฒนาอยู่แล้ว — ที่นี่เราเรียนกฎของ Java
English
The Java skeleton
- A tiny Java program looks like the example below. Your code goes inside main.
System.out.println(...)prints one line of text (like Python'sprint).- Every statement ends with a semicolon
;.
ไทย
โครงสร้างของ Java
- โปรแกรม Java ขนาดเล็กดูเหมือนตัวอย่างด้านล่าง โค้ดของคุณจะอยู่ ภายใน main
System.out.println(...)พิมพ์ข้อความหนึ่งบรรทัด (เช่น Python'sprint)- Every statement ลงท้ายด้วย semicolon
;
public class Main {
public static void main(String[] args) {
System.out.println("Java runs from inside main.");
int year = 12;
System.out.println("Grade " + year);
}
}
English
Types
- Java needs a type for every variable:
int(whole number),double(decimal),boolean(true/false),String(text). - You write the type first:
int score = 10;. - Join text and values with
+:"Grade " + yearmakesGrade 12.
ไทย
Types
- Java ต้องการ type สำหรับทุก variable:
int(whole number),double(decimal),boolean(true/false),String(text) - คุณเขียน type ไว้ก่อน:
int score = 10; - เชื่อมต่อข้อความและค่าด้วย
+:"Grade " + yearทำให้เกิดGrade 12
English
Common mistakes
- Every statement ends with
;. mainmust be exactlypublic static void main(String[] args).
ไทย
ข้อผิดพลาดที่พบบ่อย
- Every statement ลงท้ายด้วย
; mainต้องเป็นpublic static void main(String[] args)อย่างแม่นยำ
English
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
ไทย
ลองดูเลย
- Each task เติม skeleton class ไว้ล่วงหน้า — เขียนโค้ดของคุณ ภายใน main, หรือเติมเต็ม method ที่แสดง
- กด Run เพื่อ compile และ run, แล้วกด Check answer
- โค้ดของคุณ compile และ run บน server, ดังนั้นแม้การ run ครั้งแรกก็เร็ว
Inside main, use System.out.println(...) to print exactly Hello, AP CSA! · ภายใน main, ใช้ System.out.println(...) เพื่อพิมพ์ Hello, AP CSA! อย่างแม่นยำ
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete the method square(int n) so it returns n * n. The checker calls it with several values. · เติมเต็ม method square(int n) เพื่อให้ return n * n. Checker จะเรียกมันด้วยหลายค่า
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่