char

In Java, the char primitive type represents a single character. It can store letters, digits, and symbols using unicode, and occupies 2 bytes of memory.

Declaring a char

A char is declared using the char keyword. Its value is enclosed in single quotes (‘) — note that this is different from a String, which uses double quotes (“).

char letter = 'A'; 
char digit = '9';  
char symbol = '@'; 

Common mistakes:

char c = "A";   // ❌ Wrong (String)
char c = 'AB';   // ❌ Too many characters

Getting the numeric value (unicode)

You can get the unicode numeric value of a character by casting it to int:

char letter = 'A';
System.out.println((int) letter); // 65

You can represent a character in unicode. Unicode characters can be written using \u followed by four hexadecimal digits.:

char letterA = '\u0041'; // A
System.out.println(letterA);

Why unicode matters

  • Unicode supports many languages and symbols (e.g., accented letters, emojis, math symbols)
  • ASCII is a subset of Unicode
  • Java char supports values from 0 to 65535 (16-bit unicode)

Char limitations vs String

  • A char stores only one 16-bit unicode code unit
  • Although it works for most common characters, some characters (like emojis) require two char values (surrogate pairs)

Emoji Limitation:

char c = '😊'; // ❌ Compilation error
String emoji = "😊"; // ✅ Use String to store these characters:
System.out.println(emoji); // 😊

Rule of thumb:

  • Use char for single letters, digits, symbols
  • Use String for text, emojis, user input

Primitive data type

  • char is a primitive data type, and does not have methods
  • char stores its value directly in memory, unlike Objects (like String) which store a reference to the value

Character wrapper class

Although char has no methods, it has a wrapper class, Character, which provides methods that it can use.

Explanation:

  1. Primitive char: Stores a single Unicode character directly in memory (2 bytes).
  2. Wrapper class Character: Provides methods to operate on char values, such as checking, converting, or comparing characters.

Examples of Character methods

Checking / Validating

MethodExampleResult
isLetterCharacter.isLetter('A')Returns true or false
isDigitCharacter.isDigit(‘7’)Returns true or false
isLetterOrDigitCharacter.isLetterOrDigit(‘7’)Returns true or false
isUpperCaseCharacter.isUpperCase(‘A’)Returns true or false
isLowerCaseCharacter.isLowerCase(‘a’)Returns true or false

Converting characters

MethodExampleResult
toUpperCaseCharacter.toUpperCase(‘a’)Converts lowercase to uppercase
toLowerCaseCharacter.toLowerCase(‘A’)Converts uppercase to lowercase

Compare characters

MethodExampleResult
compareCharacter.compare(‘a’, ‘b’)Returns 0 if equal, negative if first < second, positive if first > second

Converting strings to char[] array

String s = "hello";
char[] chars = s.toCharArray(); // {'h','e','l','l','o'}

This is useful when processing individual string characters.

Summary

  • char stores a single character in memory
  • char values use single quotes: ‘A’, ‘1’, ‘@’
  • You can get a unicode value via casting to int
  • char is a primitive and has no direct methods. It uses Character class wrapper methods
  • A char stores only one 16-bit unicode code unit. Use String for more complex characters, like emojis

Further reading


Leave a Reply

Your email address will not be published. Required fields are marked *

This site builds beginner confidence through fundamental coding concepts and regular practice. Java is the primary language, but the techniques apply across all programming languages.

Learn. Practice. Master

Categories