tl;dr
String s = Character.toString( 128_567 ) ;
??
Details
You asked for an object of Character
, String
, or some implementation of CharSequence
.
Character
The 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
cannot.
String
?? Character.toString( int )
To get a String
object containing a single character determined by an int
, pass the int
to Character.toString()
.
As an example, we use FACE WITH MEDICAL MASK
, an emoji character at U+1F637 (decimal: 128,567).
// -----| 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
Convert that int
primitive variable to a String
.
// -----| 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
The code above works, as String
is an implementation of CharSequence
.
CharSequence cs = Character.toString( 128_567 ) ; // Returns a `String` which is a `CharSequence`.
appendCodePoint
The StringBuilder
class offers a method appendCodePoint
to add a character via its assigned Unicode code point number. Ditto for thread-safe StringBuffer
.