In Java, Character.isJavaIdentifierPart(ch) returns true when the character may appear anywhere after the first position of a Java identifier. It allows everything isJavaIdentifierStart() allows, plus digits, combining marks (accents), and non-spacing marks.
It also returns true for every character where isIdentifierIgnorable is true: certain control characters and invisible format characters. The compiler accepts those inside names and ignores them when comparing identifiers.
A static method of java.lang.Character (Java 1.1+): call it on the class, as in Character.isJavaIdentifierPart(...); you do not need a Character object. No import is needed, because java.lang is always available. The int codePoint overload was added in Java 5.
Unicode version.
The answer comes from the Unicode Character Database built into your JDK, and each Java release follows a specific Unicode version (Java 8, for example, uses Unicode 6.2). The characters used on this page have had the same properties for many Unicode versions, so the examples print the same result on any current JDK.
Syntax
Pass a char, or an int code point.
Java
Character.isJavaIdentifierPart('7'); // true, digits are fine after the start
Character.isJavaIdentifierPart('_'); // true
Character.isJavaIdentifierPart('-'); // false
Character.isJavaIdentifierPart('\u0301'); // true, combining acute accent
Character.isJavaIdentifierPart('\u0000'); // true, an ignorable control character
Call
What it does
Character.isJavaIdentifierPart(name.charAt(i))
Is the char at i allowed in a name?
Character.isJavaIdentifierPart(c) ? c : '_'
Replace illegal chars when generating a name.
Character.isJavaIdentifierPart(cp)
The same test for a code point above U+FFFF.
Two overloads: isJavaIdentifierPart(char ch) since Java 1.1 and isJavaIdentifierPart(int codePoint) since Java 5. It replaces the deprecated isJavaLetterOrDigit(char).
Library declarations
Java
// java.lang.Character
public static boolean isJavaIdentifierPart(char ch) // Java 1.1
public static boolean isJavaIdentifierPart(int codePoint) // Java 5
Parameters
Parameter
Description
ch (char)
The character to test.
codePoint (int)
A Unicode code point; invalid values return false.
Return Value
Return Value
Description
true
A letter, digit, currency symbol, connector, letter number, combining or non-spacing mark, or an ignorable character.
Replace, do not delete. The hyphen, space, and '!' become underscores, so the words stay separated.
Fix the first char."9 lives" becomes 9_lives, which still starts with a digit; inserting '_' makes it _9_lives.
Already valid.price$ passes every check, because '$' is a legal identifier part.
Example 3 — A Name with an Invisible Character Still Passes
Insert ZERO WIDTH SPACE (U+200B) into a name. Every char passes the test, but the String is no longer "abc".
Java
public class CharacterIsJavaIdentifierPartHidden {
public static void main(String[] args) {
String sneaky = "ab\u200Bc";
boolean allParts = true;
for (int i = 0; i < sneaky.length(); i++) {
if (!Character.isJavaIdentifierPart(sneaky.charAt(i))) allParts = false;
}
System.out.println("every char is a part: " + allParts);
System.out.println("length: " + sneaky.length());
System.out.println("equals abc: " + sneaky.equals("abc"));
System.out.println("U+200B ignorable: " + Character.isIdentifierIgnorable('\u200B'));
}
}
Output
every char is a part: true
length: 4
equals abc: false
U+200B ignorable: true
How It Works
An invisible char. U+200B is a FORMAT character with no width, so the text looks like abc but has 4 chars.
Accepted as a part. Because isIdentifierIgnorable('\u200B') is true, isJavaIdentifierPart also returns true for it.
Strings still differ. The compiler ignores such chars when comparing names, but String.equals does not, so validators should strip or reject them.
Frequently Asked Questions
Letters, digits, $, _, letter numbers, combining and non-spacing marks, plus ignorable characters such as some control and format chars.
The Java Language Specification treats identifier-ignorable characters as allowed but ignored, so this method includes everything isIdentifierIgnorable accepts.
No. Digits, combining marks, and ignorable chars are parts but not starts.
Not always. The first char must pass isJavaIdentifierStart, and the name must not be a keyword such as int or class.
isJavaIdentifierPart. The old method has been deprecated since Java 1.1.