Do characters have intrinsic int values in Java?

Why does this code print 97? I haven't assigned 97 to 'a' anywhere else in my code before

public static void permutations(int n) {
    System.out.print('a' + 0);
}

Solution

The type of a is char and chars can be implicitly converted to int. A is represented by 97 because this is the code point of the small Latin letter A

System.out.println('a'); // this will print out "a"

// If we cast it explicitly:
System.out.println((int)'a'); // this will print out "97"

// Here the cast is implicit:
System.out.println('a' + 0); // this will print out "97"

The first call calls println (char), and the others call println (int)

In what encoding is a Java char stored in?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>