skip to content

What characters are legal in a Java identifier, and what are the rules for where they can appear?

level: juniorimportance: must knowfreq 70%

answer

  1. Start: letter, $, or _ — never a digit
  2. Continue: add digits to the start set
  3. No spaces, no hyphens, no keywords
  4. Case-sensitive: total != Total
  5. Character.isJavaIdentifierStart / isJavaIdentifierPart

basics

~10 s

A Java identifier can contain letters, digits, the dollar sign ($), and the underscore (_). It can start with a letter, $, or _, but it cannot start with a digit.

solid answer

~40 s

A Java identifier is the name you give to things like variables, methods, classes, and packages. The legal characters are letters, digits, the dollar sign ($), and the underscore (_). The first character must be a letter, $, or _ — it must not be a digit. After the first character you may also use digits. Identifiers cannot contain spaces, hyphens, or other punctuation, and they cannot be a Java reserved keyword (like int, class, for). Identifiers are case-sensitive, so count and Count are different. By convention $ and _ are avoided at the start of names in normal code even though they are legal.

go deeper

for a junior

Can list the legal characters and state that names cannot start with a digit, with no spaces and no keywords.

for a middle

Adds case sensitivity, the keyword/literal restriction, and that there is no length limit.

for a senior

Explains the two-part start-vs-part rule and references Character.isJavaIdentifierStart/Part as the formal definition.

for a principal

Frames identifier rules in terms of lexer tokenization and the JLS, and can discuss why the rules exist (unambiguous tokenizing).

## What an identifier is An **identifier** is simply a *name* you choose for something in your program: a variable, a method, a class, an interface, a package, a label, etc. When you write `int age = 30;`, the word `age` is an identifier. Java needs rules for these names so the compiler can tell where a name starts and ends and can distinguish names from keywords and numbers. ## The two-part rule Java splits the rule into two parts: 1. **The first character** must be a *Java letter*. In practice that means: a letter (`a`–`z`, `A`–`Z`), a dollar sign `$`, or an underscore `_`. It may **not** be a digit. The reason is that if a name could start with a digit, the compiler couldn't tell `123abc` apart from a number. 2. **Every character after the first** must be a *Java letter or digit*: any of the first-character options **plus** the digits `0`–`9`. So `count`, `_count`, `$count`, `count1`, `myVar2` are all legal. `1count` is **illegal** (starts with a digit). `my-var` is illegal (hyphen is the minus operator, not a letter). `my var` is illegal (a space ends the name). ## What is *not* allowed - **No spaces** inside a name. - **No operators or punctuation** other than `$` and `_` — so no `-`, `+`, `.`, `#`, `@`, `!`, etc. The dot `.` is a separator (e.g. `obj.field`), not part of a name. - **No reserved keywords** as the whole name: you cannot name a variable `int`, `class`, `for`, `return`, etc. These are reserved by the language. (You *can* use a keyword as part of a longer name, e.g. `myClass`.) - **No boolean/null literals** as names: `true`, `false`, and `null` are literals, not identifiers, so they are also off-limits. ## Case sensitivity Java is **case-sensitive**, so `total`, `Total`, and `TOTAL` are three different identifiers. This matters constantly in practice — a typo in capitalization is a different name, not the same one. ## Length There is **no practical length limit** on an identifier; you can make names as long as you like. The limit is readability, not the compiler. ## Where this comes from The formal definition lives in the Java Language Specification. Two `Character` methods mirror the rules exactly: `Character.isJavaIdentifierStart(c)` tells you if `c` can begin an identifier, and `Character.isJavaIdentifierPart(c)` tells you if `c` can appear after the first character. These are the authoritative checks the compiler effectively uses. ## Putting it together Think of the rule as: *start with a letter/`$`/`_`, then continue with letters/digits/`$`/`_`, no spaces, no keywords, case matters.* Get that right and every identifier you write will compile.

  • Can an identifier be the same as a keyword like int or class?
    No. Reserved keywords (and the literals true, false, null) cannot be used as identifiers, though they may appear as part of a longer name like intValue.
  • Which Character methods correspond to the identifier rules?
    Character.isJavaIdentifierStart(c) for the first character and Character.isJavaIdentifierPart(c) for subsequent characters.

saying these in an interview costs you the question

  • Saying an identifier can start with a digit
  • Claiming hyphens or spaces are allowed in names
  • Forgetting that Java is case-sensitive
  • Saying $ and _ are illegal (they are legal, just discouraged)

context