-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeywords.java
More file actions
129 lines (121 loc) · 4.84 KB
/
Copy pathKeywords.java
File metadata and controls
129 lines (121 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package Phase1_CoreLanguage.Keywords;
/**
* Java Keywords (Reserved Words)
* ------------------------------
* Keywords are words RESERVED by the Java language. You cannot use them as
* identifiers (variable, class, method names). Java has 50+ reserved words,
* grouped here by purpose.
* <p>
*
* 1) Primitive Types (8)
* ----------------------
* boolean, byte, char, short, int, long, float, double
* <p>
*
* 2) Flow Control (11)
* --------------------
* if, else, switch, case, default,
* for, while, do,
* break, continue, return
* <p>
*
* 3) Class / Object Related (10)
* ------------------------------
* class, interface, enum, extends, implements,
* new, this, super, instanceof, package
* <p>
*
* 4) Access Modifiers (3)
* -----------------------
* public, private, protected
* (the fourth - "default" / package-private - has NO keyword)
* <p>
*
* 5) Non-Access Modifiers (7)
* ---------------------------
* static, final, abstract, synchronized,
* transient, volatile, native, strictfp
* (strictfp became implicit in Java 17, but it is still a reserved word)
* <p>
*
* 6) Exception Handling (5)
* -------------------------
* try, catch, finally, throw, throws
* <p>
*
* 7) Miscellaneous (4)
* --------------------
* void - method returns nothing
* import - bring another class/package into scope
* assert - debug-time invariant check (Java 1.4+)
* const, goto - RESERVED but UNUSED, kept reserved so you cannot use them
* as identifiers (Java does NOT actually support goto/const).
* <p>
*
* 8) Reserved Literals (3) - technically literals, not keywords
* -------------------------------------------------------------
* true, false, null
* <p>
*
* 9) Contextual Keywords (Restricted - act as keywords ONLY in certain contexts)
* -----------------------------------------------------------------------------
* var (Java 10+) - local variable type inference
* yield (Java 14+) - switch expression result
* record (Java 16+) - immutable data class
* sealed, non-sealed, permits (Java 17+) - sealed class hierarchies
* _ (Java 9 deprecated, Java 21+ reserved as unnamed pattern variable)
* <p>
*
* These are NOT fully reserved - you CAN still name a variable "var" or "yield"
* if you really want to (though it is confusing). They are only treated as
* keywords in the specific syntactic positions where they apply.
* <p>
*
* Why "const" and "goto" are Reserved but Unused
* -----------------------------------------------
* They were left reserved on purpose, so that the compiler can produce a
* helpful error message ("goto is not supported") instead of treating them as
* regular identifiers. The Java designers chose final + labeled break/continue
* over const + goto for safer, more readable code.
* <p>
*
* This class only demonstrates that you CANNOT use keywords as identifiers.
* The illegal lines are commented - uncomment any of them to see the compile
* error from the javac compiler.
*/
public class Keywords {
// --- Legal use of keywords in their proper place ---
public static final int MAX = 100; // public + static + final
private volatile boolean running = true; // private + volatile
protected transient String cached; // protected + transient
// --- Illegal: keywords as identifiers (uncomment to see errors) ---
// int class = 10; // ERROR: 'class' is a keyword
// int new = 10; // ERROR: 'new' is a keyword
// int return = 10; // ERROR: 'return' is a keyword
// int goto = 10; // ERROR: 'goto' is reserved (unused but reserved)
// int const = 10; // ERROR: 'const' is reserved (unused but reserved)
// int true = 10; // ERROR: 'true' is a literal
// --- Contextual keywords - actually allowed as identifiers (but confusing) ---
static int var = 5; // legal because "var" is contextual, not reserved
static int yield = 7; // legal for the same reason
public static void main(String[] args) {
// demonstrate flow-control keywords
for (int i = 0; i < 3; i++) {
if (i == 1) continue; // 'continue' keyword
if (i == 2) break; // 'break' keyword
System.out.println("i = " + i);
}
// exception handling keywords
try {
throw new RuntimeException("boom"); // 'throw' keyword
} catch (RuntimeException e) { // 'catch' keyword
System.out.println("caught: " + e.getMessage());
} finally { // 'finally' keyword
System.out.println("always runs");
}
// OUTPUT
// i = 0
// caught: boom
// always runs
}
}