-
(char)
Code:
public class prg {
public static void main(String[]args) {
int i;
i=10;
char a = 'n';
System.out.println((char)('n'+i)); //<---------------
}
}
The output I get is x
Similarly if it is (char)('n'+2),output is p
I really don't get it.What is happening here? What does (char) actually do?
-
Re: (char)
char is an integer describing a characters code. So you can modify it by adding integers to get other characters.
EDIT: (char) casts an integer to a character interpreting it as character code, google "java type casting" maybe to get more info about casts.
-
Re: (char)
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
"char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive)."
Unicode Zeichentabelle
n = 6e hex = 110 dec
110 + 10 = 120 dec = 78 hex = x