I have seen Questions and Answers about obtaining the code point number of a Unicode character in Java. For example, the Question How can I get a Unicode character’s code?.
But I want the opposite: given an integer number, how do I get text of that character assigned to that code point number?
char primitive data type is of no use, being limited to only the Basic Multilingual Plane of the Unicode character set. That plane represents approximately the first 64,000 characters defined in Unicode. But Unicode has grown to nearly double that, over 113,000 characters defined now. The numbers assigned to characters range over a million. Being based on 16-bits, a
char is limited to a range of 64K, not nearly enough.
When writing string literals, we can use a Unicode escape sequence with the backslash-with-u. But I am interested in working with integer variables, soft-coding rather than hardcoding the Unicode characters.
String s = Character.toString( 128_567 ) ;
You asked for an object of
String, or some implementation of
Character class is actually legacy, a mere object wrapper around the primitive
char type. The
char type is legacy too, being defined internally as a 16-bit number limited to the first 64K of Unicode code points. Unicode now has more than twice than number of code points assigned to characters, so
char fails to represent most characters.
So we cannot instantiate a
Character object for a character outside the Basic Multilingual Plane set of characters. So, as a workaround,
Character.toString( int ) produces a
String containing a single character.
String can handle any and all Unicode characters, while
Character.toString( int )
// -----| input |---------------- String input = "😷" ; // FACE WITH MEDICAL MASK at code point U+1F637 (decimal: 128,567). int codePoint = input.codePointAt( 0 ) ; // Returns 128,567. System.out.println( "codePoint : " + codePoint ) ;
codePoint : 128567
int primitive variable to a
// -----| String |---------------- String output = Character.toString( codePoint ) ; // Pass an `int` primitive integer number. System.out.println( "output : " + output ) ;
output : 😷
Or use a literal integer number.
String output2 = Character.toString( 128_567 ) ; // Pass an integer literal. System.out.println( "output2 : " + output2 ) ;
output2 : 😷
See this code run live at IdeOne.com.
CharSequence cs = Character.toString( 128_567 ) ; // Returns a `String` which is a `CharSequence`.