How does the charArray[i] represents both the char and its decimal value in the result of print(charArray[i]) method without any value for char method can anyone help?
how does it work?
How does the charArray[i] represents both the char and its decimal value in the result of print(charArray[i]) method without any value for char method can anyone help?
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
6 July 2023, 06:15
A char is a numerical value (16 bit, 2 byte -> in the end a unsigned short). So when you assign a 'C' to a char variable, your program saves internally the numeric value 67 (as this number represents the character C, search eg. for ascii table).
This you can see if you cast char to an int (that's allowed because char is numeric and less precise that int)
output:
When you print a character using a method Java offers, then JVM looks into a codepage (like that ascii table) and translates the number back to a character and outputs it. Java uses UTF-16.
If you look at the print method, then you'll see it takes an int as argument. Means the char is casted to an int (like I showed you above). Now you have the codepoint (int value) that points to the connected character (the char, when you cast the int back to a char).
0
Anonymous #11352715
7 July 2023, 08:03
Thank you for clear explanation.
0