J2EEOnline J2EEOnline


Java Fundamentals  «Prev  Next»
Lesson 7Literal values
Objective Literal values used with each primitive, String, and other object types.

Java Literal Values

Integer literals

Identify the literal values that may be used with each primitive type, the String type, and other object types.
The values of the byte, short, int, and long types may be written as decimal, hexadecimal, or octal numbers. Hexadecimal numbers are preceded by 0x or 0X and use the letters A - F (upper or lower case) to represent the decimal numbers 10 through 15. Octal numbers begin with a 0. The suffixes l or L are appended to numbers to indicate that they are long values.

Floating point literals

Floating point numbers may be written using a decimal point (e.g., 123.456). They may also use exponential notation, which consists of an e or E followed by a positive or negative exponent. For example, 2.3e5d represents 2.3 x 105. The suffixes f and F indicate float values. The suffixes d and D represent double values. For a value to be recognized as a float or double, it should have a decimal point, exponent, or a suffix. If an f or F suffix is not used, a value is assumed to be a double.

Character literals

A character literal may be written by putting the character within single quotes ('). Certain characters are written using the escape codes shown in the following table:

Escape code Character
\b backspace
\t tab
\n new line
\f form feed
\r carriage return
\' single quote
\\ backslash
\" double quote

Other Unicode characters may be written with a backslash followed by an 8-bit octal value (\000 through \377) or by a u or U followed by a four-digit hexadecimal value (\u0000 through \uffff).

String literals

String literals are written as characters enclosed by double quotes. Character escape codes may be used within String literals.

The null value

The literal value null is used to indicate that a variable of an object type does not reference an object.

Java literal - Quiz

Click the Quiz link to check your understanding of primitive types and literal values.
Java literal - Quiz