Incremental Java
Primitive Types: Char

Char Types

We've looked at numeric types: int and double. However, computers do more than process numbers. They process text. If you use a word processor or practically anything where you need to write "words", you know why we need types that handle things that look like words.

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.

Char Literals

To write a character in Java, write a single quote, the character, and another single quote. For example, 'A' is a char literal. So is 'a' (although it's different because it's lower case). So is '3'. This is not the same as 3, which is an int. Java stores '3' in the box in a different way than 3 even though both look about the same.

' ' 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.

Not Char Literals

"3" is not a char literal. It uses double quotes, instead of single quotes. (Double quotes makes it a String which we'll discuss later).

'' 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.

Escape Sequences

There are a few characters that would be hard to see if we just typed it. For example, suppose you want to write the char literal for the return key. Think about it. You hit return, and the cursor moves to the next line. How might you write this?

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.

Other Escape Sequences

I'll admit, escape sequences are a bad name. It's hard to figure out what is meant by this name. Basically, you are "escaping" or leaving the literal meaning of backslash, and using it to indicate something special is happening.

Let's look at a few more escape sequences.

tab

'\t' is the character you get when you press the tab key. Usually, this has the effect of getting to the next tab stop. The tab key, like the newline, isn't really a character. It is a command, stored as a character. Think about a word processor. When you type a tab, it just moves the cursor. (Even though a space key also doesn't print anything, it's reasonable to pretend that it is printing something, even though it's invisible).

backslash

Since backslash is a special symbol that starts an escape sequence, how can we store a real backslash? Occasionally, we might wish to print a backslash, right?

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.

single quote

What if you wanted a char literal a single quote? Is this OK? '''. This is three quotes in a row.

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?

double quote

You can write double quotes as '"'. Hard to read, but it works. It does not need an escape sequence (though I believe there's no harm in using a backslash).

Table

Here's a table of results.

Escape Sequence Meaning
'\n' newline
'\t' tab
'\\' backslash
'\'' single quote

Open and Close Quote

When you see 'a', the first single quote is called the open quote. It starts off a char literal. The second quote is called the close quote. It finishes the char literal.

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.

Review

In your own words, write a definition for an escape sequence. Also, write the table above, without looking.