Programming References and tutorials for Developers.

Java Literals

By The Saint on Wednesday, February 11, 2009

Filed Under:

Java Literals
In Java, literals refer to fixed values that are represented in their human-readable form. For example, the number 100 is a literal. Literals are also commonly called constants. For the most
part, literals, and their usage, are so intuitive that they have been used in one form or another by all the preceding sample programs. Now the time has come to explain them formally.

Java literals can be of any of the primitive data types. The way each literal is represented depends upon its type. As explained earlier, character constants are enclosed in single quotes. For example, 'a' and ' %' are both character constants.

Integer constants are specified as numbers without fractional components. For example, 10 and –100 are integer constants. Floating-point constants require the use of the decimal point followed by the number’s fractional component. For example, 11.123 is a floating-point constant.
Java also allows you to use scientific notation for floating-point numbers.

By default, integer literals are of type int. If you want to specify a long literal, append an l or an L. For example, 12 is an int, but 12L is a long. By default, floating-point literals are of type double. To specify a float literal, append an F or f to the constant. For example, 10.19F is of type float.

Although integer literals create an int value by default, they can still be assigned to variables of type char, byte, or short as long as the value being assigned can be represented by the target type. An integer literal can always be assigned to a long variable.

Integer Literals
Integer literals come in different formats: decimal (base 10), hexadecimal (base 16), and octal (base 8). In using integer literals in our program, we have to follow some special notations.

For decimal numbers, we have no special notations. We just write a decimal number as it is. For hexadecimal numbers, it should be preceded by “0x” or “0X”.

For octals, they are preceeded by “0”. For example, consider the number 12. It's decimal representation is 12, while in hexadecimal, it is 0xC, and in octal, it is equivalent to 014.

Integer literals default to the data type int. An int is a signed 32-bit value. In some cases, you may wish to force integer literal to the data type long by appending the “l” or “L” character. A long is a signed 64-bit value. We will cover more on data types later.

Floating-Point Literals
Floating point literals represent decimals with fractional parts. An example is 3.1415. Floating point literals can be expressed in standard or scientific notations. For example, 583.45 is in standard notation, while 5.8345e2 is in scientific notation. Floating point literals default to the data type double which is a 64-bit value. To use a smaller precision (32-bit) float, just append the “f” or “F” character.

Character Literals
Character Literals represent single Unicode characters. A Unicode character is a 16-bit character set that replaces the 8-bit ASCII character set. Unicode allows the inclusion of symbols and special characters from other languages. To use a character literal, enclose the character in single quote delimiters. For example, the letter a, is represented as ‘a’.

To use special characters such as a newline character, a backslash is used followed by the character code. For example, ‘\n’ for the newline character, ‘\r’ for the carriage return, ‘\b’ for backspace.

String Literals
String literals represent multiple characters and are enclosed by double quotes. An example of a string literal is, “Hello World”.

Boolean Literals
Boolean literals have only two values, true or false.

Reference: Jedi Courseware

0 comments for this post