In this piece of code:
public class TestChar {
public static void main(String[] args) {
char c = 'A';
int n = c;
System.out.println(n);
}
}
Why does it print 65? Is it the Unicode value of the character 'A'?
In this piece of code:
public class TestChar {
public static void main(String[] args) {
char c = 'A';
int n = c;
System.out.println(n);
}
}
Why does it print 65? Is it the Unicode value of the character 'A'?
'A' is the character literal A (Unicode code value 65)
The reason it prints 65
instead of A
is overloading: the method println
is actually a number of different methods, distinguished by the arguments they take. There are these two among others:
println(char) - prints the value as a character
println(int) - prints the value as a number
Internally the representation of the char 'A' and the int 65 is the same. The only difference is that the two types have different storage sizes. However, when you pass them to println
the type is used to select which variant of the method to call.