The problem is that a Character
object represents a char
; i.e. a number on the range 0
through 0xffff
. Unicode code-points range up to U+10FFFF
and many cannot be represented as a single char
value.
So this gives you a problem:
If the code-points that you want to represent are all between U+0000
and U+FFFF
, then you can represent them as Character
values.
If any are U+10000
or larger, then it won't work.
So, if you have an int
that represents a Unicode code-point, you need to do do something like this:
int value = ...
if (Character.isDefined(value)) {
if (value <= 0xffff) {
return Character.valueOf((char) value);
} else {
// code point not representable as a `Character`
}
} else {
// Not a valid code-point at all
}
Note:
int
values that are not valid code points include negative values, values greater than 0x10ffff
and lower and upper surrogate code-units.
- A number of commonly used Unicode code-points are great than U+10000. For example, the code-points for Emojis! This means that using
Character
is a bad idea. It would be better to use either a String
, a char[]
or an Integer
.
It seems to work so far.
I guess you haven't tried @Shawn's approach with an Emoji yet.
Is there a way around using a downcast?
No.
if(i == 0)
throw new NullPointerException();
That is just wrong:
Zero is a valid code-point.
Even if it wasn't valid, it is NOT a null
. So throwing NullPointerException
is totally inappropriate.
If you are concerned about the case where i
is null
, don't worry. Any operation that unboxes i
will automatically throw NullPointerException
if it is null
. Just let it happen ...