Which commonly-mistaken Java words are NOT keywords (e.g. String, System, main, length)? How do you tell a keyword from a library identifier?
answer
- String/System/main/args/length = NOT keywords (library/identifiers)
- Keywords = fixed grammar list, lowercase, ~50
- int String = 5; compiles (shadows the class)
- int class = 5; never compiles
- Test: if it works as a variable name, it's not a keyword
basics
~20 sWords like String, System, main, args, and length are NOT keywords — they're class names, method names, or fields from the libraries. Keywords are the fixed grammar words like class, public, and void. You could even reuse the library names as your own (though it's a bad idea).
solid answer
~40 sA frequent confusion is treating familiar library names as keywords. `String`, `System`, `Object`, `Integer` are class names in `java.lang`; `main`, `println`, `length` are method or field names; `args` is just a parameter name the convention uses. None are reserved — you could legally write `int String = 5;` (the compiler allows it, though it shadows the class and is terrible style). Real keywords (`class`, `public`, `static`, `void`, `if`, `return`) are reserved by the grammar and can never be identifiers. The way to tell them apart: keywords are a fixed, small list baked into the language and always lowercase; library identifiers are defined in the JDK's classes and you can find, import, or even redefine them. A quick test — if `int word = 5;` compiles, `word` is not a keyword.
code
java · 9 linespublic static void main(String[] args) {
// keywords here: public, static, void
// identifiers here: main, String, args
int String = 5; // legal! 'String' is not reserved (now shadows the class)
System.out.println(String);
// int void = 5; // illegal: 'void' IS a keyword
}go deeper
Can separate the grammar words (public/static/void) from the library names (String/main/args) in a typical signature.
Explains the shadowing test and knows non-keywords are ordinary identifiers definable in classes.
Articulates the conceptual boundary (grammar vs. library), name resolution/shadowing implications, and why keywords are lowercase and finite.
Discusses scoping and name-resolution order (how shadowing a type vs. a variable resolves), and tooling/lint policies that forbid shadowing standard types.
## The confusion Beginners often see `public static void main(String[] args)` and assume **every** word in it is a keyword. In reality only some are. Telling them apart is a core fundamentals skill. ## What IS a keyword in that line - `public` — access modifier keyword. - `static` — modifier keyword. - `void` — return-type keyword. These are reserved by the grammar. ## What is NOT a keyword in that line - `main` — just a **method name**. The JVM looks for a method literally named `main`, but the word itself is an ordinary identifier; you could name another method `main` elsewhere. - `String` — a **class** in the `java.lang` package. It's an identifier, not a keyword. - `args` — a **parameter name**, pure convention; `String[] arguments` works identically. ## Other commonly-mistaken non-keywords - `System`, `Object`, `Integer`, `Math`, `Thread` — **class names** in the standard library. - `println`, `length`, `equals`, `toString`, `hashCode` — **method/field names**. - `out`, `in`, `err` — **fields** of `System`. ## The decisive test Because non-keywords are ordinary identifiers, you can (legally, if unwisely) reuse them: ```java int String = 5; // compiles! 'String' is shadowed as a local variable ``` This compiles because `String` is not reserved — though it now hides the `java.lang.String` class within that scope, which is confusing and bad style. By contrast `int class = 5;` never compiles. **So the practical test is:** if `int someWord = 5;` compiles, `someWord` is not a keyword. ## How to tell them apart conceptually - **Keywords**: a fixed, finite, lowercase list (~50) built into the **language grammar**. You can't import or redefine them. - **Library identifiers**: names defined in **classes** (the JDK or your own). They're discovered via `import`, can be many, often capitalized (classes) by convention, and can be shadowed. ## Why this matters Knowing the boundary prevents two mistakes: (1) thinking you can't name something `string` (lowercase) when in fact you can, and (2) accidentally shadowing important classes like `String` or `List` and producing baffling errors. The clean rule: **keywords are grammar; everything capitalized-and-importable is library.**
- Is `main` a keyword?No. `main` is just a method name the JVM looks for as the program entry point. The word itself is an ordinary identifier you could use elsewhere.
- Why is it bad to name a variable `String` even though it compiles?It shadows the java.lang.String class in that scope, so you can no longer refer to the type by that name there — leading to confusing errors and unreadable code.
Keywords are the fixed grammar of English (the, and, if); library names like String are vocabulary words from a dictionary — you can even name your pet 'String' if you want, but you can't name it 'and' and expect sentences to work.
saying these in an interview costs you the question
- Claiming String, System, or main are keywords (they are library identifiers)
- Saying you cannot name a variable 'string' or even 'String' (you can — they are not reserved)
- Believing that capitalization makes a word a keyword (keywords are actually all lowercase)