⬅ Previous Topic
Java catch KeywordNext Topic ⮕
Java class Keyword⬅ Previous Topic
Java catch KeywordNext Topic ⮕
Java class Keywordchar
Keyword in JavaIn Java, the char
keyword is used to declare a variable that holds a single 16-bit Unicode character. It’s a primitive data type and is commonly used to store letters, symbols, or any character defined in the Unicode standard.
char
?Characters are a vital part of string manipulation, input/output operations, and user interfaces. The char
type allows direct storage of character values without the overhead of full String
objects, making it a lightweight option when you only need a single character.
char variableName = 'A';
The character must be enclosed in **single quotes**. Double quotes are used for String
literals and will cause a compilation error if used with char
.
public class CharExample {
public static void main(String[] args) {
char grade = 'A';
System.out.println("Grade: " + grade);
}
}
Grade: A
Explanation: Here, the variable grade
holds the character 'A'
and prints it out. Simple and efficient.
public class UnicodeChar {
public static void main(String[] args) {
char unicodeChar = '\u263A'; // Unicode for ☺
System.out.println("Unicode Character: " + unicodeChar);
}
}
Unicode Character: ☺
Explanation: Java char
supports Unicode, so you can represent global characters and symbols using \u
followed by a four-digit hexadecimal code.
char
Some characters can't be typed directly or have special meanings. Java allows escape sequences to represent them:
'\n'
– New Line'\t'
– Tab'\''
– Single Quote'\"'
– Double Quote'\\'
– Backslashpublic class EscapeCharExample {
public static void main(String[] args) {
char quote = '\"';
System.out.println("He said: " + quote + "Hello!" + quote);
}
}
He said: "Hello!"
Explanation: We used an escape character to store and display a double quote inside the output string.
Since characters are internally represented by numeric Unicode values, you can perform arithmetic operations on them.
public class CharArithmetic {
public static void main(String[] args) {
char ch = 'A';
ch++;
System.out.println("Next character: " + ch);
}
}
Next character: B
Explanation: 'A'
has a Unicode value of 65. Incrementing it gives 66, which corresponds to 'B'
.
char
In Java, if a char
is declared as a class variable and not initialized, its default value is '\u0000'
(the null character).
public class DefaultChar {
static char defaultChar;
public static void main(String[] args) {
System.out.println("Default char value: [" + defaultChar + "]");
}
}
Default char value: []
Explanation: It prints a blank because '\u0000'
is a non-printable character.
char
char c = "A";
→ ❌)char c = 'AB';
→ ❌)The char
keyword is simple yet powerful. It gives direct access to individual characters, supports Unicode, and allows for operations like comparison and arithmetic. Whether you're formatting output, parsing input, or manipulating text, char
is an essential part of your Java toolbox.
Write a Java program that prints the alphabets from A to Z using a loop. Hint: Use char
and character arithmetic.
⬅ Previous Topic
Java catch KeywordNext Topic ⮕
Java class KeywordYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.