[Solved] Error: empty character literal

In this post, I will be sharing how to fix the error: empty character literal in Java. When we need to declare an empty char, usually, we use ' ' (empty single quotes) as a char. I have already shared 3 other ways to represent empty char in Java. As always, first, we will produce the error empty character literal before moving on to the solution.

Read Also:  Comparing characters in Java

[Fixed] Error: empty character literal

Example 1: Producing the error by printing the empty single quotes character


We can easily produce this error by printing the ' ' (empty single quotes) character as shown below:

public class EmptyCharacterLiteral {
    public static void main(String args[]) {
      char ch = '';
      System.out.println("Print empty char" + ch);
    }
}

Output:
/EmptyCharacterLiteral.java:3: error: empty character literal
             char ch = ' ';
                              ^
1 error



Explanation:

The cause of this compilation error is due to trying to print empty character in Java. As a result, we were getting an error: empty character literal.

Solution:

To overcome this compilation error, we can assign the char data type variable ch with the values Character.MIN_VALUE, or '\u0000', or '\0', as shown below:

public class EmptyCharacterLiteral {
    public static void main(String args[]) {
      // Assign null character to ch variable    
      char ch = Character.MIN_VALUE;
      //OR
      //char ch = '\u0000';
      //OR
      //char ch = '\0';
      System.out.println("Print empty char" + ch); 
    }
}

Output:
Print empty char


Example 2: Producing the error by comparing the empty single quotes character


We can easily produce this error by comparing the ' ' (empty single quotes) character as shown below:

public class EmptyCharacterLiteral2 {
    public static void main(String args[]) {
      char ch = 'a';
      // Comparing null character to ch variable
      if (ch == ('')) 
        System.out.println("char is: " + ch);
    }
}

Output:
/EmptyCharacterLiteral2.java:5: error: empty character literal
             if (ch == (''))
                              ^
1 error



Explanation:

The cause of this compilation error is due to trying to compare empty single quotes character with the char data type variable ch. As a result, we were getting error: empty character literal.

Solution:

To overcome this compilation error, we can replace the empty single quotes character with the values Character.MIN_VALUE, or '\u0000', or '\0', as shown below:

public class EmptyCharacterLiteral2 {
    public static void main(String args[]) {
      char ch = 'a';
      // Comparing null character to ch variable
      if (ch == Character.MIN_VALUE)// OR (ch == '\u0000') OR (ch == '\0') 
        System.out.println("char is: " + ch);
    }
}

Output:
               (No Output Printed)


That's all for today. Please mention in the comments in case you are still facing the error: empty character literal in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry