- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java char Keyword
Usage and Examples
char
Keyword in Java
In 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.
Why Use 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.
Syntax
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
.
Basic Examples
Example 1: Storing a Character
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.
Example 2: Using Unicode Values
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.
Escape Characters in 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'\\'
– Backslash
Example 3: Escape Sequences
public 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.
Character Arithmetic
Since characters are internally represented by numeric Unicode values, you can perform arithmetic operations on them.
Example 4: Incrementing a Character
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'
.
Default Value of char
In Java, if a char
is declared as a class variable and not initialized, its default value is '\u0000'
(the null character).
Example 5: Default char Value
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.
Common Mistakes with char
- Using double quotes instead of single quotes (e.g.,
char c = "A";
→ ❌) - Assigning multiple characters (e.g.,
char c = 'AB';
→ ❌) - Forgetting that char holds Unicode, not just ASCII
Conclusion
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.
Practice Challenge
Write a Java program that prints the alphabets from A to Z using a loop. Hint: Use char
and character arithmetic.