Java
Character class
empty char
programming
coding

How to represent an empty char in Java

Codemia Knowledge Hub ·

Java has no empty char: every primitive char holds exactly one 16-bit UTF-16 code unit. The usual stand-in is the null character '\u0000', which is also the default value of an uninitialized char and equal to Character.MIN_VALUE. If you need a real "no value", use the Character wrapper and set it to null.

Background: char and Character

The Character class in java.lang wraps a primitive char in an object and provides static methods for inspecting characters. A char itself occupies 16 bits and can never be "nothing", which is why text-processing and parsing code needs a convention for "no character here".

Options for an empty character

The null character '\u0000'

This is the default value of an uninitialized char. It is not truly empty, but it signals that no explicit character was set.

java
   char nullChar = '\u0000';

Character.MIN_VALUE

Character.MIN_VALUE is the smallest char value and is the same as '\u0000'. It can read more clearly than the escape sequence.

java
   char emptyChar = Character.MIN_VALUE;  // Equivalent to '\u0000'

null with the Character wrapper

A Character is an object, so unlike a primitive char it can be null. This is the closest thing to a genuinely absent value.

java
   Character emptyCharObj = null;

A whitespace character

If you only need something invisible in output, a space ' ' or another whitespace character such as the non-breaking space '\u00A0' works. This is a display choice, not a marker for "no value".

Checking for the empty marker

Here, user input is simulated as the null character and checked before use:

java
1public class EmptyCharExample {
2    public static void main(String[] args) {
3        char inputChar = '\u0000'; // Simulating empty input
4        if (inputChar == '\u0000') {
5            System.out.println("Empty character");
6        } else {
7            System.out.println("Character: " + inputChar);
8        }
9    }
10}

Useful Character methods for this kind of check:

  • Character.isWhitespace(char): checks whether the character is whitespace.
  • Character.isLetter(char): checks whether the character is a letter.
  • Character.valueOf(char): converts a primitive char to a Character instance.

Quick reference

ConceptRepresentationDescription
Null character'\u0000'Default uninitialized char, not truly empty, but signifies "no value".
Whitespace' ' or other Unicode spacesUsed for representing invisible characters visually.
Character minimum valueCharacter.MIN_VALUEEquivalent to '\u0000', signifies the smallest value in char.
Null character objectnullWhen using Character objects, null signifies the absence of a character.

Which one to pick depends on your logic: '\u0000' for a primitive placeholder, null when absence must be explicit, and whitespace only when the goal is visual.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms