Is there a method in Java that mimics a cast to a character?

To simplify some analysis of bytecode, I want to replace the explicit (down) casts of primitive types in a Java program with calls to an equivalent method of type boxed. In most cases, this is simple:

double d = 42.0;
int i = (int)d;

Can be converted to

Double tmp = new Double(d);
int i = tmp.intValue();

This works for all signed types. But charand Characterthese methods do not. That is, Integer, Float, etc. They have methods intValue, floatValueetc., But none of them has a method charValue. Is there a jdk way to go from primitive type to char / Character?

So basically there is something like this:

Integer num = new Integer(65);
char ch = num.charValue();

And the key should not be used (char).

+4
2

-, , char. char , int. JVMS Β§4.9.2:

, int, boolean, byte, char short.

. 2.3.4 Β§ 2.2.11, Java boolean, byte, short char int.)

, , , int char Java , -, " " int. - char - int, . :

char ch = num.charValue();

( ):

int ch = num.intValue() & 0x0000FFFF;

, ch char (, , a char char).

, , , i2c int to char, , .

, , () double ( ) char.

float double byte, short char, int, Integer.MAX_VALUE Integer.MIN_VALUE, int, . . JLS Β§5.1.3 - . , byte, short char, , .

+3

, - , char. :

Integer num = new Integer(65);
char ch = (char)num.intValue();

, . , , .

+1

Source: https://habr.com/ru/post/1665811/


All Articles