Gaurang Tandon Gaurang Tandon - 3 years ago 273
Java Question

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.

Answer Source

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';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download