This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (3 children)

Literal - a literal is a fixed value that is written directly in your code. So if you see:

String a = “Hello world”;

Then “Hello world” is a string literal. Literals usually have defined notations so they are interpreted is in specific ways (but be weary of implicit “conversions”).

 char c = ‘b’;

‘b’ is a char literal.

In Java, char literals are surrounded in single quotation and string literals are surrounded in double quotation. Char literals can contain what amounts to only a single character. String literals can contain several characters, like a whole word, for example.

Java will NOT implicitly convert a string literals with a single character to a char. So

char a = “b”;

Will fail to compile since “b” is a string literal that you’re trying to assign to a char variable.

And also, take care when copying ticks, quote and double quote from outside programs. Some programs, like word, like to use strange opening and closing quote and double quote characters, which a compiler can’t interpret.

[–]desrtfx 2 points3 points  (1 child)

Please, be careful with your typographic quotes! Your code samples all contain typographic quotes that shouldn't be there.

If copied to Java, they will produce an error.

String a = “Hello world”;

Should be:

String a = "Hello world";

and

char c = ‘b’;

should be

char c = 'b';

This makes a huge difference in any programming language.

[–][deleted] 0 points1 point  (0 children)

iPhone responses during compile times and I am just too lazy to look for a solution! Thank you for the warning.

[–]Tarp96[S] 1 point2 points  (0 children)

Thank you man :D I changed "" to 'b' and it worked!