Character Java Identifier Validation
Validate Java identifiers with Character.isJavaIdentifierStart and isJavaIdentifierPart; they accept letters, digits, underscore, dollar sign, Unicode letters.
Code
public class IdentifierCheck {
// 标识符规则:首字符 isJavaIdentifierStart,其余 isJavaIdentifierPart
static boolean isValidIdentifier(String name) {
if (name == null || name.isEmpty()) {
return false;
}
if (!Character.isJavaIdentifierStart(name.charAt(0))) {
return false;
}
for (int i = 1; i < name.length(); i++) {
if (!Character.isJavaIdentifierPart(name.charAt(i))) {
return false;
}
}
return true;
}
public static void main(String[] args) {
for (String candidate : new String[] {"userId", "_temp2", "2fast", "user id", "$price", "café"}) {
System.out.println(candidate + " -> " + isValidIdentifier(candidate));
}
}
}How it works
Everything happens inside the class IdentifierCheck and the functions isValidIdentifier and main.
A single loop drives the file — for (int i = 1; i < name.length(); i++).
Points of care
- It runs on exactly what the toolchain already ships — nothing extra.
Where you would put it to use
The signature you will reach for first is static boolean isValidIdentifier(String name)
Execute the file and main walks through isValidIdentifier, in that order.
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.UUID Version DetectorValidate the 8-4-4-4-12 UUID layout and read its version (v1 to v8) and variant nibbles per RFC 9562, with full field decomposition.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
FAQ
- How do you run Character Java Identifier Validation?
- The file is self-contained: run it directly, no dependencies to install beforehand.
Report a problem with this exampleOpens a pre-filled issue on GitHub.