Java Programming/Literals

Java Literals are syntactic representations of boolean, character, numeric, or string data. Literals provide a means of expressing specific values in your program. For example, in the following statement, an integer variable named  is declared and assigned an integer value. The literal  represents, naturally enough, the value zero.

The code section 3.62 contains two number literals followed by two boolean literals at line 1, one string literal followed by one number literal at line 2, and one string literal followed by one real number literal at line 3:

Boolean Literals
There are two boolean literals


 * represents a true boolean value
 * represents a false boolean value

There are no other boolean literals, because there are no other boolean values!

Numeric Literals
There are three types of numeric literals in Java.

Integer Literals
In Java, you may enter integer numbers in several formats:


 * 1) As decimal numbers such as ,   . Negative decimal numbers such as   are actually expressions consisting of the integer literal with the unary negation operation.
 * 2) As octal numbers, using a leading   (zero) digit and one or more additional octal digits (digits between   and  ), such as  . Octal numbers may evaluate to negative numbers; for example   is actually the decimal value -8.
 * 3) As hexadecimal numbers, using the form   (or  ) followed by one or more hexadecimal digits (digits from   to ,   to   or   to  ). For example, 0xCAFEBABEL is the long integer 3405691582. Like octal numbers, hexadecimal literals may represent negative numbers.
 * 4) Starting in J2SE 7.0, as binary numbers, using the form   (or  ) followed by one or more binary digits (0 or 1). For example, 0b101010 is the integer 42. Like octal and hex numbers, binary literals may represent negative numbers.

By default, the integer literal primitive type is. If you want a, add a letter el suffix (either the character  or the character  ) to the integer literal. This suffix denotes a long integer rather than a standard integer. For example,  is a long integer literal. Long integers are 8 bytes in length as opposed to the standard 4 bytes for. It is best practice to use the suffix  instead of   to avoid confusion with the digit   (one) which looks like   in many fonts:   &ne;. If you want a short integer literal, you have to cast it.

Starting in J2SE 7.0, you may insert underscores between digits in a numeric literal. They are ignored but may help readability by allowing the programmer to group digits.

Floating Point Literals
Floating point numbers are expressed as decimal fractions or as exponential notation:

Floating point numbers consist of:
 * 1) an optional leading   or   sign, indicating a positive or negative value; if omitted, the value is positive,
 * 2) one of the following number formats
 * 3) * integer digits (must be followed by either an exponent or a suffix or both, to distinguish it from an integer literal)
 * 4) * integer digits
 * 5) * integer digits  integer digits
 * 6) *  integer digits
 * 7) an optional exponent of the form
 * 8) * the exponent indicator  or
 * 9) * an optional exponent sign  or   (the default being a positive exponent)
 * 10) * integer digits representing the integer exponent value
 * 11) an optional floating point suffix:
 * 12) * either  or   indicating a single precision (4 bytes) floating point number, or
 * 13) *  or   indicating the number is a double precision floating point number (by default, thus the double precision (8 bytes) is default).

Here, integer digits represents one or more of the digits  through.

Starting in J2SE 7.0, you may insert underscores between digits in a numeric literal. They are ignored but may help readability by allowing the programmer to group digits.

Character Literals
Character literals are constant valued character expressions embedded in a Java program. Java characters are sixteen bit Unicode characters, ranging from 0 to 65535. Character literals are expressed in Java as a single quote, the character, and a closing single quote. Character literals have the type, an unsigned integer primitive type. Character literals may be safely promoted to larger integer types such as and. Character literals used where a or  is called for must be cast to  or  since truncation may occur.

String Literals
String literals consist of the double quote character (ASCII 34, hex 0x22), zero or more characters (including Unicode characters), followed by a terminating double quote character, such as:

So a string literal follows the following grammar: 

Within string and character literals, the backslash character can be used to escape special characters, such as unicode escape sequences, or the following special characters:

String literals may not contain unescaped newline or linefeed characters. However, the Java compiler will evaluate compile time expressions, so the following String expression results in a string with three lines of text:

null
is a special Java literal which represents a null value: a value which does not refer to any object. It is an error to attempt to dereference the null value &mdash; Java will throw a. is often used to represent uninitialized state.

Mixed Mode Operations
In concatenation operations, the values in brackets are concatenated first. Then the values are concatenated from the left to the right. Be careful when mixing character literals and integers in String concatenation operations:

The unexpected results arise because and  are converted twice. The expression is concatenated as such:

The code section 66 yields the desired result:
 * 1)   and   are integers. So they store integer values. The integer value of  is 49 and the integer value of  is 48.
 * 2) So the first concatenation concatenates  and  .   is first converted into String, yielding  and the concatenation returns the string.
 * 3) The second concatenation concatenates  and .  is converted into the String  and the concatenation returns the string.
 * 4) The concatenation between  and  returns the string.

Question 3.9: Consider the following code:

Explain the results seen.

For the first line:

For the second line:

For the last line: