Literals

Literals are pieces of Java source code that mean exactly what they say. For instance "Hello World!" is a String literal and its meaning is the words Hello World!

The string "Hello World!" looks like it's several things; but to the compiler it's just one thing, a String. This is similar to how an expression like 1,987,234 may be seven digits and two commas but is really just one number.

The double quote marks tell you this is a string literal. A string is an ordered collection of characters (letters, digits, punctuation marks, etc.). Although the String may have meaning to a human being reading the code, the computer sees it as no more than a particular set of letters in a particular order. It has no concept of the meaning of the characters. For instance it does not know that "two" + "two" is "four." In fact the computer thinks that "two" + "two" is "twotwo"

The quote marks show where the string begins and ends. However the quote marks themselves are not a part of the string. The value of this string is Hello World!, not "Hello World!" You can change the output of the program by changing Hello World to some other line of text.

A string in a Java program has no concept of italics, bold face, font family or other formatting. It cares only about the characters that compose it. Even if you're using an editor like NisusWriter that lets you format text files, "Hello World!" is identical to "Hello World!" as far as Java is concerned.

char literals are similar to string literals except they're enclosed in single quotes and must have exactly one character. For example 'c' is a char literal that means the letter c.

true and false are boolean literals that mean true and false.

Numbers can also be literals. 34 is an int literal and it means the number thirty-four. 1.5 is a double literal. 45.6, 76.4E8 (76.4 times 10 to the 8th power) and -32.0 are also double literals.

34L is a long literal and it means the number thirty-four. 1.5F is a float literal. 45.6f, 76.4E8F and -32.0F are also float literals.


Previous | Next | Top | Cafe au Lait

Copyright 1997, 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified February 3, 1999