Singham Singham - 1 year ago 105
Java Question

How to convert a non-primitive Integer to non-primitive Character in java?

I have:

Integer i = Integer.valueOf(0);

How do I convert it into a Character?

Character.valueOf(i); // requires a char in argument
and thus doesn't work.

I want to use the primitive data type minimum times in the conversion.

Answer Source

You can try Character#toChars(int) and discard the superfluous character, if there is one generated (see the documentation for more infos on behavior of the method).

//just a stub to show how the code works. Might not work in all cases!!!
Integer i = ...;
Character c = Character.toChars(i)[0];

Note though that this is only a cosmetic action involving quite a load of autoboxing. The JavaAPI is built around the primitives and Integer, Character and other wrapper-classes are merely more than a mixture of utility-classes and (as the name suggests) wrappers for primitives.

In general java is based on the primitive-types, so avoiding them would be a workaround for the way the language is designed. Rather unnatural IMO.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download