Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

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:

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

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.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You 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.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M