0

:)I am pretty new to coding! My question is as following:

I have a method which gets a Integer Object i as a parameter and this Integer is supposed to be a Unicode. Now my question is, is there a way to transform a given Unicode to a Characterobject? A little example to illustrate the question:

public Character randomName(Integer i) {
    if(i == 0)
            throw new NullPointerException();
    if(i > Character.MAX_VALUE)
            throw new FormatException(i);
    else {
            Character c = new Character(i);
            return c;
}

of course this doesn't work but i can't find a way around it.... Downcasting didnt work and I usually use char and int but in this case i have to do it that way.

Thanks for the help already!!

1 Answers1

1

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:

  1. int values that are not valid code points include negative values, values greater than 0x10ffff and lower and upper surrogate code-units.
  2. 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:

  1. Zero is a valid code-point.

  2. Even if it wasn't valid, it is NOT a null. So throwing NullPointerException is totally inappropriate.

  3. 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 ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Wow, thanks! Well, my method is supposed to throw exceptions if the value is e.g negative or out of the Character's Max Value. So, it only turns the integer into a char after it checked if it is even possible. Yes a String would also be a lot easier but my task clearly wants a Charakter..... Thanks a lot!!! – Serkan Besim Feb 01 '22 at 13:59
  • Sounds like your task is incorrect! But I guess there are few people who teach Java and don't understand Unicode properly. – Stephen C Feb 01 '22 at 14:03