คำสงวน (JAVA Keywords)

คำสงวน (JAVA Keywords)

คำสงวน (Keywords or Reserved Word) คือ คำที่ถูกสงวน หรือสำรองไว้โดยตัวแปลภาษา ที่ผู้พัฒนาไม่สามารถนำมาใช้เป็นชื่อตัวแปร ชื่อเมธอด หรือชื่อคลาส เพราะคำสงวนจะมีหน้าที่ของตนเอง ในการควบคุมโครงสร้างของโปรแกรม (Structure) เป็นชนิดของข้อมูล (Data Type) หรือคำขยายแบบของข้อมูล (Modifier) ดังนั้นตัวแปลภาษาจะประกาศคำที่ถูกสงวนไว้ล่วงหน้า มิให้ผู้พัฒนานำไปใช้ และเกิดความสับสนในการพัฒนาโปรแกรม

คำสงวน 49 คำ ประกอบด้วย abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while

แหล่งอ้างอิง http://www.thaiall.com/class/indexsign.htm

การแบ่งกลุ่มคำสงวนแบบ 4 กลุ่ม

DATA = 13 CONTROL = 18 OBJECTS = 16 UNUSED = 2
1. boolean

2. byte

3. char

4. double

5. float

6. int

7. long

8. short

9. final

10. static

11. void

12. strictfp

13. transient

 

1. assert

2. break

3. case

4. catch

5. continue

6. default

7. do

8. else

9. finally

10. for

11. if

12. return

13. switch

14. synchronized

15. throw

16. throws

17. try

18. while

1. abstract

2. class

3. extends

4. implements

5. import

6. instanceof

7. interface

8. native

9. new

10. package

11. private

12. protected

13. public

14. super

15. this

16. volatile

 

1. const

2. goto

 

การแบ่งกลุ่มคำสงวนแบบ 9 กลุ่ม

  1. ขยายการเข้าถึง (Access Modifiers)
  2. ขยายคลาส เมธอด และตัวแปร (Class, Method, and Variable Modifiers)
  3. ควบคุมการไหลในโปรแกรม (Flow Control)
  4. การจัดการกับข้อผิดพลาด (Error Handling)
  5. ควบคุมแพกเกจ (Package Control)
  6. แบบข้อมูลดั่งเดิม (Primitives)
  7. คำหลักตัวแปร (Variable Keywords)
  8. คำแบบไม่คืนค่า (Void Return Type Keyword)
  9. คำที่ไม่ใช้ (Unused Reserved Words)
  10. ขยายการเข้าถึง (Access Modifiers)
  11. private : Makes a method or a variable accessible only from within its own class.
  12. protected : Makes a method or a variable accessible only to classes in the same package or subclasses of the class.
  13. public : Makes a class, method, or variable accessible from any other class.
  14. ขยายคลาส เมธอด และตัวแปร (Class, Method, and Variable Modifiers)
  15. abstract : Used to declare a class that cannot be instantiated, or a method that must be implemented by a nonabstract subclass.
  16. class : Keyword used to specify a class.
  17. extends : Used to indicate the superclass that a subclass is extending.
  18. final : Makes it impossible to extend a class, override a method, or reinitialize a variable.
  19. implements : Used to indicate the interfaces that a class will implement.
  20. interface : Keyword used to specify an interface.
  21. native : Indicates a method is written in a platform-dependent language, such as C.
  22. new : Used to instantiate an object by invoking the constructor.
  23. static : Makes a method or a variable belong to a class as opposed to an instance.
  24. strictfp : Used in front of a method or class to indicate that floating-point numbers will follow FP-strict rules in all expressions.
  25. synchronized : Indicates that a method can be accessed by only one thread at a time.
  26. transient : Prevents fields from ever being serialized. Transient fields are always skipped when objects are serialized.
  27. volatile : Indicates a variable may change out of sync because it is used in threads.
  28. ควบคุมการไหลในโปรแกรม (Flow Control)
  29. break : Exits from the block of code in which it resides.
  30. case : Executes a block of code, dependent on what the switch tests for.
  31. continue : Stops the rest of the code following this statement from executing in a loop and then begins the next iteration of the loop.
  32. default : Executes this block of code if none of the switch-case statements match.
  33. do : Executes a block of code one time, then, in conjunction with the while statement, it performs a test to determine whether the block should be executed again.
  34. else : Executes an alternate block of code if an if test is false.
  35. for : Used to perform a conditional loop for a block of code.
  36. if : Used to perform a logical test for true or false.
  37. instanceof : Determines whether an object is an instance of a class, superclass, or interface.
  38. return : Returns from a method without executing any code that follows the statement (can optionally return a variable).
  39. switch : Indicates the variable to be compared with the case statements.
  40. while : Executes a block of code repeatedly while a certain condition is true.
  41. การจัดการกับข้อผิดพลาด (Error Handling)
  42. catch : Declares the block of code used to handle an exception.
  43. finally : Block of code, usually following a try-catch statement, which is executed no matter what program flow occurs when dealing with an exception.
  44. throw : Used to pass an exception up to the method that called this method.
  45. throws : Indicates the method will pass an exception to the method that called it.
  46. try : Block of code that will be tried, but which may cause an exception.
  47. assert : Evaluates a conditional expression to verify the programmer’s assumption.
  48. ควบคุมแพกเกจ (Package Control)
  49. import : Statement to import packages or classes into code.
  50. package : Specifies to which package all classes in a source file belong.
  51. แบบข้อมูลดั่งเดิม (Primitives)
  52. boolean : A value indicating true or false.
  53. byte : An 8-bit integer (signed).
  54. char : A single Unicode character (16-bit unsigned)
  55. double : A 64-bit floating-point number (signed).
  56. float : A 32-bit floating-point number (signed).
  57. int : A 32-bit integer (signed).
  58. long : A 64-bit integer (signed).
  59. short : A 16-bit integer (signed).
  60. คำหลักตัวแปร (Variable Keywords)
  61. super : Reference variable referring to the immediate superclass.
  62. this : Reference variable referring to the current instance of an object.
  63. คำแบบไม่คืนค่า (Void Return Type Keyword)
  64. void : Indicates no return type for a method.
  65. คำที่ไม่ใช้ (Unused Reserved Words)
  66. const : Do not use to declare a constant; use public static final.
  67. goto : Not implemented in the Java language. It’s considered harmful.