简体   繁体   中英

why char ch =4 (without '') , is not error?

I want to know why char ch =5; (for example) is not error ? but if I print

System.out.println(Character.isDigit(ch));
// output 

false

it will be false ?

Because 5 is an integer literal that can be converted to a char . It is not the character '5' however.

A character is represented by two bytes in memory. Java converts 5 to a character. '5' is not the 6th character (its hexadecimal code is 35 and not 5) in the ASCII table and is thus not a "digit".

try this example :

 char ch = 97;
 JOptionPane.showMessageDialog(null,"ch = "+ch);

The answer would be : ch = a

It simply won't give an error even though 97 is without (' ') because 97 represent the ASCII code for the character 'a' so it's not a digit , and that's why you are getting false as a result.

如果你给ch = 5 ,它会根据ASCII值自动转换为char。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM