Primitive character

You will use primitive character most of the time for a single character value.

char ch = 'a';
 
// character supports Unicode encoding
// you can represent Unicode characters using their hex values
char unicodeChar = '\u0041'; // Unicode value for capital letter 'A'

Arithmetic operation on character

In java, char is an unsigned 16-bit integer that represents a ASCII character. More about character literal on Character literal.

char character = 97;
System.out.println(character);
a

The Unicode numerical value for lower case letter ‘a’ is 97.

When you perform arithmetic operations on characters, you are actually performing arithmetic on their ASCII value.

// 'a' has a Unicode value of 97, whereas 'b' is 98
char result = 'b' - 'a';
// this code is equivalent to 98 - 97
System.out.println(result);
1

In the below code, ch has a value of ‘a’, which has an equivalent ASCII value of 97, when we increment the value, it represents a different character.

char ch = 'a';
ch++;
System.out.println(ch);
b

Character wrapper class


Back to parent page: Java Standard Edition (Java SE) and Java Programming

Web_and_App_DevelopmentProgramming_LanguagesJavaCharacter

Reference: