当前位置: 动力学知识库 > 问答 > 编程问答 >

java - 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 uses double quotes for "String" and single quotes for 'C'haracters.

网友答案:

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.

网友答案:

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

Change

y = 'hello';

-->

y = "hello";
分享给朋友:
您可能感兴趣的文章:
随机阅读: