Java has a char type. char stands for character. Characters aren't just the alphabetic characters. They include digits, spaces, punctuation, and so forth. Any character you can type and see on the screen, and even characters you can't see are called char.
' ' is also a char. It is a blank space. So is '?'. Nearly every character you see on the keyboard is a character, and even a few you don't.
'' is not a char literal. There isn't a character between the two single quotes. You need one character between the double quotes.
'ab' is not a char literal. There are two characters in between the single quotes. char literals only have one character in between.
In Java, you write this as '\n'.
"Wait a minute", you say. "You told me that you could only have one character between single quotes. I see two!"
You're right. However, the backslash has a special meaning. In Java (and C and C++), the backslash tells Java "Wait, I'm not a real backslash. I'm a special symbol that starts an escape sequence. Look at the following character to see what special character I am".
(OK, it doesn't say this, but you'll admit that it makes this stuff easier to learn!)
When Java sees '\n', it sees a backslash. It knows this is the start of an escape sequence. It looks at the next character which is an n. When put together, these two characters stand for the newline character. The newline character is the "character" that appears when you hit the return key. (In reality, the newline character is not a character, but a command, telling the display to move the cursor to the beginning of the next line. However, we store this command as a character).
Unfortunately, '\n' is newline for only UNIX machines. PCs use two characters, '\r' and '\n', which is carriage return and linefeed. Rather than confuse you, we'll just stick with '\n' for newline.
Let's look at a few more escape sequences.
The answer is simple: '\\'. Put two backslashes in a row. The first backslash still means "I'm special, look at the next character to figure out the escape sequence". The next backslash says "I'm a backslash".
Escape sequences are almost always two characters long. The first is always a backslash. The second is a character who's meaning is now changed because of the previous backslash.
That doesn't work. Java sees the first quote and thinks "You're starting a char literal". It sees the second quote and thinks "You're ending a char literal. Wait a minute! Why are you ending a char literal. I need a character in between! Error! Error!"
As you can see, Java isn't very smart about this.
The solution is the backslash. '\''. Now Java sees it this way. "I see a single quote. That starts off a char literal. I see a backslash. That starts off an escape sequence. I'll look at the next character to see what the escape sequence is. It's another single quote. So that must mean I am looking at a true single quote, instead of the end of a char literal. Finally, I see another single quote, which is the end of the char literal. That's not a true single quote. It just ends the literal."
Interesting how much processing goes on, right?
Escape Sequence | Meaning |
'\n' | newline |
'\t' | tab |
'\\' | backslash |
'\'' | single quote |
The quotes are just ways for you to tell Java where a character begins and ends. They are not part of the character.
Quotes are sort of like those yellow sticky note. For example, suppose you are leaving a book for a friend to pick up. You put a sticky note on the book. The note isn't the book. It's just a way of telling you which book to look at. Similarly, the single quotes aren't part of the character. They just tell you where the character begins and ends.
That doesn't mean you can leave them off. If they weren't there, Java wouldn't know they were char literals.