There is something I don't understand about how to convert Java from type char to type int.

Asked 1 years ago, Updated 1 years ago, 398 views

I don't understand why Java's conversion from char to int changes to that value.
The JRE I use is the JRE System Library java11.
getNumeric and -'0' are two common methods used in Java to convert char to int.
However, I searched and found that even if the given char does not represent a valid digit, the above method does not produce an error.
Among them, I do not understand 'Even if char does not represent a valid digit, the above method does not produce an error'.
Therefore, please explain why the output of ch5 to ch8 does not output an integer of 65 even if you convert it as follows.

Source Code

class CharVariable{
    public static void main(String[]args) {

        // A is printed.
        char1 = 'A';
        char2 = 65;

        System.out.println("ch1="+ch1);
        System.out.println("ch2="+ch2);

        // Convert Char type to an integer of 65.number one
        char3 = 'A';
        char4 = 65;

        int convert 3 = ch3;
        int convert 4 = ch4;

        System.out.println("ch3="+convert3);
        System.out.println("ch4="+convert4);

        // Convert Char type to an integer of 65.the second
        char5 = 'A';
        char6 = 65;

        System.out.println("ch5="+Character.getNumericValue(ch5));
        System.out.println("ch6="+Character.getNumericValue(ch6));

        // Convert Char type to an integer of 65.the third
        char7 = 'A';
        char8 = 65;

        int convert 7 = ch7 - '0';
        int convert8 = ch8 - '0';

        System.out.println("ch7="+convert7);
        System.out.println("ch8="+convert8);
    }

}

Output Results

ch1=A
ch2 = A
ch3 = 65
ch4 = 65
ch5 = 10
ch6 = 10
ch7 = 17
ch8 = 17

java

2022-09-30 22:05

1 Answers

Character.getNumericValue(char),Character.getNumericVFor value(int), follow the linked API specification.

The uppercase letter A-Z ('\u0041'-'\u005A'), (omitted) has a number of 10-35.

Therefore, 'A' and 65 (=0x41) are converted to 10 just as described in the specification above.

(Typically, the alphabet is used to represent values greater than or equal to 10 with radix greater than 10(Example: hexadecimal) But this seems to be the right specification for that purpose.)

In addition,

in the questionnaire.

The above method does not produce errors even if char does not represent a valid digit.

That's probably because you can find it at the link above.

-2 if the character contains a number but cannot be represented as a nonnegative int value.-1 if the characters do not have numbers.

I understand that this has nothing to do with this story.

Then, the reason why 'A'-'0' is 17 is simply by looking at the Unicode code list.
You can see that 0 is 0x30, and A is 0x41.
In other words,

 'A' - '0'
= 0x41 - 0x30
= 0x11 
= 17 (in decimal)

That's it.

For type conversion from char to int, see also the following link:

< u l >
  • Why replacing the char type with int type does not result in compilation errors - Stack overflow

  • 2022-09-30 22:05

    If you have any answers or tips


    © 2024 OneMinuteCode. All rights reserved.