Represent Empty Char in Java

Mohammad Irfan Nov 08, 2021 Oct 09, 2021
  1. Creating Empty Char in Java
  2. Creating Empty Char by Passing Null Char in Java
  3. Creating Empty Char by Using a Unicode Value in Java
  4. Creating Empty Char by Using MIN_VALUE Constant in Java
Represent Empty Char in Java

This tutorial introduces how to represent empty char in Java.

In Java, we can have an empty char[] array, but we cannot have an empty char because if we say char, then char represents a character at least, and an empty char does not make sense. An empty char value does not belong to any char, so Java gives a compile-time error.

To create an empty char, we either can assign it a null value \0 or default Unicode value \u0000. The \u0000 is a default value for char used by the Java compiler at the time of object creation.

In Java, like other primitives, a char has to have a value. Let’s understand with some examples.

Creating Empty Char in Java

Let’s create an empty char in Java and try to compile the code. See in the below example, we created a single char variable ch and printed its value to check whether it has anything to print, and see it throws a compile-time error.

public class SimpleTesting{
	public static void main(String[] args){
		char ch = '';
		System.out.println(ch);
	}
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Invalid character constant

The above code example fails to compile because of an invalid character constant. It means Java does not recognize the empty char, but if we assign a char having a space, the code compiles successfully and prints an empty space to the console.

We must not confuse space char with empty char as both are different, and Java treats them differently. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		char ch = ' ';
		System.out.println(ch);
	}
}

Creating Empty Char by Passing Null Char in Java

This is another solution that can be used to create empty char and can avoid code compilation failure. Here, we used \0 to create empty char and it works fine. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		char ch = '\0';
		System.out.println(ch);
	}
}

Creating Empty Char by Using a Unicode Value in Java

We can use \u0000 value to create an empty char in Java. The Java compiler uses this value to set as char initial default value. It represents null that shows empty char. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		char ch1 = '\u0000';
		System.out.println(ch1);
	}
}

Creating Empty Char by Using MIN_VALUE Constant in Java

Java Character class provides a MIN_VALUE constant that represents the minimum value for a character instance. In Java, the minimum value is a \u0000 that can be obtained using the MIN_VALUE constant of the Character class and can be assigned to any char variable to create an empty char. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		char ch = Character.MIN_VALUE;
		System.out.println(ch);
	}
}

If you are working with the Character class, you can directly use null literal to create an empty char instance in Java. As the Character class is derived from the Object class, we can assign null as an instance. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		Character ch = null;
		System.out.println(ch);
	}
}

Related Article - Java Char