Character Literals

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Is each of the following a different character?

0       O         o

Answer:

Yes. Each of the above (zero, Capital 'O', and lower case 'o') is a different character and has its own 16 bit code.

Character Literals

In a program, a character literal is surrounded with an apostrophe on both sides:

'm'          'y'               'A'

In a program, control characters are represented with several characters inside the apostrophes:

'/n'          '/t'

Each of these is what you do in a program to get a single char. The first one represents the 16 bit newline character and the second one represents the tabulation character. You will rarely use any control characters other than these two. Several others are listed in the Java documentation.

Warning: The following is not a character literal:

"Hello"

This is, in fact, a String, which is not primitive data. Strings are surrounded by double quotes ", not by apostrophes.

QUESTION 14:

What is wrong with the following char literal:

"W"