简体   繁体   中英

Unclosed Character Literal error

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.

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';

Java对"String"使用双引号,对'C' "String"使用单引号。

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';

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

Change

y = 'hello';

-->

y = "hello";

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.

There are 8 primitive datatypes in Java. 在此处输入图像描述 char is one of them. When compiler sees a char datatype is defined. It allocates 1 Bytes of memory from JVM heap and expects a value after = symbol with two conditions.

  1. value enclosed inside ' (single quotes).
  2. value to be single character long. It can be either a single character or valid code corresponding single character, which you can't type using English Keyboard.

In the same way, a datatype of String type should be enclosed with " (double quotes) and can have any length characters sequence.

In the given example you have mixed concepts of both char and String datatype. the compiler clearly saying:

Unclosed Character Literal

Means, you started with ' single quote, so compiler just expects a single character after opening ' and then a closing ' . Hence, the character literal is considered unclosed and you see the error.

So, either you use char data type and ' single quotes to enclose single character. Or use String datatype and " double quotes to enclose any length of character sequence.

So, correct way is:

String y = "hello";
System.out.println(y);

Character only takes one value dude! like: char y = 'h'; and maybe you typed like char y = 'hello'; or smthg. good luck. for the question asked above the answer is pretty simple u have to use DOUBLE QUOTES to give a string value. easy enough;)

Use double quotes symbol as I mention below Your y data type is String, and it should double quotes symbol

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM