问题
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.
回答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';
回答2:
Java uses double quotes for "String"
and single quotes for 'C'
haracters.
回答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';
回答4:
'' encloses single char
, while "" encloses a String
.
Change
y = 'hello';
-->
y = "hello";
回答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.
回答6:
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;)
来源:https://stackoverflow.com/questions/17344312/unclosed-character-literal-error