Unclosed Character Literal error

124,622

Solution 1

In Java, single quotes can only take one character, with escape if necessary. You need to use full quotation marks as follows for strings:

y = "hello";

You also used

System.out.println(g);

which I assume should be

System.out.println(y);

Note: When making char values (you'll likely use them later) you need single quotes. For example:

char foo='m';

Solution 2

Java uses double quotes for "String" and single quotes for 'C'haracters.

Solution 3

I'd like to give a small addition to the existing answers. You get the same "Unclosed Character Literal error", if you give value to a char with incorrect unicode form. Like when you write:

char HI = '\3072';

You have to use the correct form which is:

char HI = '\u3072';

Solution 4

'' encloses single char, while "" encloses a String.

Change

y = 'hello';

-->

y = "hello";

Solution 5

String y = "hello";

would work (note the double quotes).

char y = 'h'; this will work for chars (note the single quotes)

but the type is the key: '' (single quotes) for one char, "" (double quotes) for string.

Share:
124,622
Gaurang Tandon
Author by

Gaurang Tandon

I love to write lots of code. IIIT-Hyderabad CSE Undergrad.

Updated on October 01, 2021

Comments

  • Gaurang Tandon
    Gaurang Tandon over 2 years

    Got the error "Unclosed Character Literal" , using BlueJ, when writing:

    class abc
    {
       public static void main(String args[])
       {
           String y;
           y = 'hello';
           System.out.println(y);
       }
    }
    

    But I can't figure out what is wrong. Any idea?

    Thanks.