HOWTO · Java

How to Represent Empty Char in Java

This article introduces how to handle empty char in Java.

On this page

Java has no empty primitive char: a char holds one UTF-16 code unit. The literal '' is invalid because it contains no character. An empty char[] is possible, but it is a zero-element array, not a char value. For no text, use an empty String; for a character value, choose a real character such as a space or U+0000.

Why '' is invalid and a space is different

A char literal uses single quotes and contains one character. The expression char ch = ''; does not compile. By contrast, ' ' is a valid char containing a space. This small program makes the space visible inside brackets and prints its numeric value:

public class CharExample {
  public static void main(String[] args) {
    char space = ' ';
    System.out.println("[" + space + "]");
    System.out.println((int) space);
  }
}
[ ]
32

The brackets distinguish the space from the surrounding text, and the integer shows its character value. A space is still present in comparisons and string operations, so use it only when whitespace is the intended data.

Use an empty String when there is no character

When data represents no text, use String value = "";. An empty String has length zero and suits text fields, concatenation, and APIs that expect a string. It is not a char, so Java does not allow assigning "" to a primitive char variable. An empty char[] is also possible, but it represents a sequence with no elements rather than one absent character.

Use \0 or \u0000 for the null character

Java spells U+0000 as \0 or \u0000; both represent the same actual char value. U+0000 is sometimes called the null character, but it is not an empty character and does not generally terminate a Java String. Printing it may look blank, so inspect its numeric value when you need to see what it contains. It still occupies one code unit and contributes to a Java string’s length().

public class NullCharacterExample {
  public static void main(String[] args) {
    char shortEscape = '\0';
    char unicodeEscape = '\u0000';
    String value = "A" + shortEscape + "B";

    System.out.println(shortEscape == unicodeEscape);
    System.out.println((int) shortEscape);
    System.out.println(value.length());
  }
}
true
0
3

The source compares the two spellings, casts U+0000 to an integer, and measures the resulting string. This makes the value checkable without relying on how a terminal renders it. The string length is three because it contains A, U+0000, and B.

Use Character.MIN_VALUE or null with Character

This runnable example wraps a letter and prints its value and presence check:

public class CharacterExample {
  public static void main(String[] args) {
    Character value = Character.valueOf('J');
    System.out.println(value);
    System.out.println(value == null);
  }
}
J
false

Character.MIN_VALUE is a named way to obtain U+0000; it is not a separate empty value. Use it when that specific character is what your data format requires. A Character wrapper can instead hold null to represent an absent value. That is a missing reference, not a char, and code must check it before unboxing or calling methods on it.

Java initializes an instance field of type char to U+0000. A local char variable has no automatic default and must be assigned before it is read. Choose String for no text, Character with null for an optional character, and \0 only when U+0000 itself is meaningful.