Java Character isJavaIdentifierPart() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Character

Definition

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
CallWhat 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

ParameterDescription
ch (char)The character to test.
codePoint (int)A Unicode code point; invalid values return false.

Return Value

Return ValueDescription
trueA letter, digit, currency symbol, connector, letter number, combining or non-spacing mark, or an ignorable character.
falseSpaces, operators, most punctuation, emoji.

Compare with Similar Methods

Related checks:

MethodHow it differs
isJavaIdentifierStart(ch)The stricter rule for the first character.
isUnicodeIdentifierPart(ch)Unicode rules: rejects '$'.
isIdentifierIgnorable(ch)The invisible chars this method also accepts.

Java Examples

Example 1 — Characters Allowed Inside a Java Name

Test a letter, a digit, both special symbols, a hyphen, a space, the NUL control char, and a combining accent.

Java
public class CharacterIsJavaIdentifierPartTable {
    static String show(char c) {
        return (c >= 32 && c < 127) ? "'" + c + "'" : "0x" + Integer.toHexString(c);
    }

    public static void main(String[] args) {
        char[] chars = {'a', '7', '_', '$', '-', ' ', '\u0000', '\u0301'};
        for (char c : chars) {
            System.out.println(show(c) + " part=" + Character.isJavaIdentifierPart(c));
        }
    }
}

How It Works

  1. Digits now pass. '7' cannot start a name, but it is allowed in every later position.
  2. A surprise: NUL. U+0000 is an ignorable control character, so isJavaIdentifierPart accepts it even though you would never type it.
  3. Accents are parts. U+0301 COMBINING ACUTE ACCENT attaches to the letter before it, so it is allowed inside a name.

Example 2 — Turn Arbitrary Text into a Java Identifier

Replace every char that is not an identifier part with '_', and add a leading '_' if the first char cannot start a name.

Java
public class CharacterIsJavaIdentifierPartSanitize {
    static String toIdentifier(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            sb.append(Character.isJavaIdentifierPart(c) ? c : '_');
        }
        if (sb.length() == 0 || !Character.isJavaIdentifierStart(sb.charAt(0))) {
            sb.insert(0, '_');
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String[] inputs = {"order-total 2024!", "9 lives", "price$"};
        for (String in : inputs) {
            System.out.println("[" + in + "] -> " + toIdentifier(in));
        }
    }
}

How It Works

  1. Replace, do not delete. The hyphen, space, and '!' become underscores, so the words stay separated.
  2. Fix the first char. "9 lives" becomes 9_lives, which still starts with a digit; inserting '_' makes it _9_lives.
  3. 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'));
    }
}

How It Works

  1. An invisible char. U+200B is a FORMAT character with no width, so the text looks like abc but has 4 chars.
  2. Accepted as a part. Because isIdentifierIgnorable('\u200B') is true, isJavaIdentifierPart also returns true for it.
  3. 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.

Next: isJavaIdentifierStart()

Check whether a character may be the first character of a Java identifier.

isJavaIdentifierStart() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful