Java Character getName() Method

The Character.getName() method in Java is used to return the name of a specified character code point.

Table of Contents

  1. Introduction
  2. getName() Method Syntax
  3. Examples
    • Getting the Name of a Valid Code Point
    • Handling Unassigned Code Points
  4. Real-World Use Case
  5. Conclusion

Introduction

The Character.getName() method is a static method in the Character class in Java. It returns the name of the character corresponding to a specified Unicode code point. If the code point is unassigned, it returns null. This method is particularly useful for debugging and logging purposes when working with Unicode characters.

getName()() Method Syntax

The syntax for the getName() method is as follows:

public static String getName(int codePoint)
  • codePoint: The Unicode code point of the character.

The method returns:

  • The name of the character corresponding to the specified Unicode code point.
  • null if the code point is unassigned.

Examples

Getting the Name of a Valid Code Point

The getName() method can be used to get the name of a character for a valid Unicode code point.

Example

public class GetNameExample {
    public static void main(String[] args) {
        int codePoint = 65; // Unicode code point for 'A'

        String name = Character.getName(codePoint);

        System.out.println("Name of code point 65: " + name);
    }
}

Output:

Name of code point 65: LATIN CAPITAL LETTER A

In this example, the method returns the name of the character corresponding to the Unicode code point 65.

Handling Unassigned Code Points

The getName() method returns null if the specified code point is unassigned.

Example

public class UnassignedCodePointExample {
    public static void main(String[] args) {
        int unassignedCodePoint = 0x110000; // An unassigned code point

        String name = Character.getName(unassignedCodePoint);

        System.out.println("Name of code point 0x110000: " + name);
    }
}

Output:

Name of code point 0x110000: null

In this example, the method returns null because the code point 0x110000 is unassigned.

Checking Multiple Code Points

You can check the names of multiple code points to see their character names or handle unassigned ones.

Example

public class MultipleCodePointsExample {
    public static void main(String[] args) {
        int[] codePoints = { 65, 66, 67, 0x110000 };

        for (int codePoint : codePoints) {
            String name = Character.getName(codePoint);
            if (name != null) {
                System.out.println("Name of code point " + codePoint + ": " + name);
            } else {
                System.out.println("Code point " + codePoint + " is unassigned.");
            }
        }
    }
}

Output:

Name of code point 65: LATIN CAPITAL LETTER A
Name of code point 66: LATIN CAPITAL LETTER B
Name of code point 67: LATIN CAPITAL LETTER C
Code point 1114112 is unassigned.

Real-World Use Case

Logging Unicode Character Information

In a real-world application, you might need to log Unicode character information for debugging or audit purposes.

Example

public class LoggingExample {
    public static void main(String[] args) {
        String text = "Hello, World! \uD83D\uDE00";

        for (int i = 0; i < text.length(); i++) {
            int codePoint = text.codePointAt(i);
            String name = Character.getName(codePoint);
            if (name != null) {
                System.out.println("Character: " + text.charAt(i) + ", Code Point: " + codePoint + ", Name: " + name);
            } else {
                System.out.println("Character: " + text.charAt(i) + ", Code Point: " + codePoint + ", Name: Unassigned");
            }
            if (Character.isSupplementaryCodePoint(codePoint)) {
                i++; // Skip the next index as it is part of the surrogate pair
            }
        }
    }
}

Output:

Character: H, Code Point: 72, Name: LATIN CAPITAL LETTER H
Character: e, Code Point: 101, Name: LATIN SMALL LETTER E
Character: l, Code Point: 108, Name: LATIN SMALL LETTER L
Character: l, Code Point: 108, Name: LATIN SMALL LETTER L
Character: o, Code Point: 111, Name: LATIN SMALL LETTER O
Character: ,, Code Point: 44, Name: COMMA
Character:  , Code Point: 32, Name: SPACE
Character: W, Code Point: 87, Name: LATIN CAPITAL LETTER W
Character: o, Code Point: 111, Name: LATIN SMALL LETTER O
Character: r, Code Point: 114, Name: LATIN SMALL LETTER R
Character: l, Code Point: 108, Name: LATIN SMALL LETTER L
Character: d, Code Point: 100, Name: LATIN SMALL LETTER D
Character: !, Code Point: 33, Name: EXCLAMATION MARK
Character:  , Code Point: 32, Name: SPACE
Character: 😀, Code Point: 128512, Name: GRINNING FACE

Conclusion

The Character.getName() method in Java is used for retrieving the name of a character corresponding to a specified Unicode code point. By understanding how to use this method, you can efficiently handle and log character information in your Java applications. Whether you are checking individual characters, handling unassigned code points, or logging Unicode character details, the getName() method provides a reliable solution for these tasks.

Comments