ความคิดเห็น Java เป็นคำสั่งที่ไม่ได้ดำเนินการโดยคอมไพเลอร์และล่าม ความคิดเห็นสามารถใช้เพื่อให้ข้อมูลเกี่ยวกับตัวแปร เมธอด คลาส หรือคำสั่งใดๆ นอกจากนี้ยังใช้เพื่อซ่อนรหัสโปรแกรมในช่วงเวลาที่กำหนดได้อีกด้วย
ประเภทของความคิดเห็น Java
ความคิดเห็นใน Java มีสามประเภท
- ความคิดเห็นบรรทัดเดียว
- ความคิดเห็นหลายบรรทัด
- ความคิดเห็นเกี่ยวกับเอกสาร
ความคิดเห็นบรรทัดเดียว
ความคิดเห็นบรรทัดเดียวเริ่มต้นด้วย // และสิ้นสุดที่ท้ายบรรทัด
ตัวอย่างที่ 1
public class SingleLineComment {
public static void main(String[] args) {
// To print output in the console
System.out.println("Welcome to Tutorials Point");
}
} ผลลัพธ์
Welcome to Tutorials Point
ความคิดเห็นหลายบรรทัด
ความคิดเห็นแบบหลายบรรทัดเริ่มต้นด้วย /* และลงท้ายด้วย */ ที่ครอบคลุมหลายบรรทัด
ตัวอย่างที่ 2
public class MultiLineComment {
public static void main(String[] args) {
/* To print the output as Welcome to Tutorials Point
in the console.
*/
System.out.println("Welcome to Tutorials Point");
}
} ผลลัพธ์
Welcome to Tutorials Point
คำอธิบายเกี่ยวกับเอกสาร
ความคิดเห็นเกี่ยวกับรูปแบบเอกสารเริ่มต้นด้วย /** และสิ้นสุดด้วย */ และครอบคลุมหลายบรรทัด ความคิดเห็นเกี่ยวกับเอกสารต้องมาก่อนคลาสหรืออินเทอร์เฟซหรือเมธอดหรือคำจำกัดความของฟิลด์ทันที
ตัวอย่างที่ 3
/**
This is a documentation comment.
This class is written to show the use of documentation comment in Java.
This program displays the text "Welcome to Tutorials Point" in the console.
*/
public class DocumentationComment {
public static void main(String args[]) {
System.out.println("Welcome to Tutorials Point");
}
} ผลลัพธ์
Welcome to Tutorials Point