Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character literal in Java?

Tags:

java

So I just started reading "Java In A Nutshell", and on Chapter One it states that:

"To include a character literal in a Java program, simply place it between single quotes" i.e.

char c = 'A'; 

What exactly does this do^? I thought char only took in values 0 - 65,535. I don't understand how you can assign 'A' to it?

You can also assign 'B' to an int?

int a = 'B'

The output for 'a' is 66. Where/why would you use the above^ operation?

I apologise if this is a stupid question.

My whole life has been a lie.

like image 286
Adz Avatar asked Dec 12 '12 22:12

Adz


1 Answers

char is actually an integer type. It stores the 16-bit Unicode integer value of the character in question.

You can look at something like http://asciitable.com to see the different values for different characters.

like image 126
Justin Niessner Avatar answered Oct 13 '22 14:10

Justin Niessner