Java char keyword
The Java char keyword is a primitive data type. It is used to declare the character-type variables and methods. It is capable of holding the unsigned 16-bit Unicode characters.
Points to remember
- The char range lies between 0 to 65,535 (inclusive).
- Its default value is ‘u0000’.
- Its default size is 2 byte.
- It is used to store characters.
Why char uses 2 bytes in java?
It is because Java uses Unicode system not ASCII code system.
What is u0000 ?
The u0000 is the lowest range of the Unicode system.
Examples of Java char keyword
Example 1
Let’s see a simple example of displaying characters.
Output:
char1: a char2: A
Example 2
In this example, we provide integer value to char variable. Here, compiler implicitly typecast integer to char and display the corresponding ASCII value.
Output:
char1: A char2: a
Example 3
In this example, we typecast the integer value to char explicitly.
Output:
char1: a char2: A
Example 4
Let’s see an example to represent the char value in Unicode System.
Output:
char1: a char2: A
Example 5
In this example, we increment the provided char value by 1.
Output:
char: B
Example 6
Let’s see an example to break the string in the form of characters.
Output:
String: TutorAspire char: [j, a, v, a, t, p, o, i, n, t]
Example 7
Let’s see an example to invoke the method of the char return type.
Output:
a