Java Literals

A literal is a constant value that the compiler interprets and converts to its own internal representation. There are literals that are used to represent the five basic data types and their variants: boolean, integer, floating-point, characters, and strings.

Boolean Literals

The boolean literals are simply true and false. These literals are used to represent the only two available logical values of a boolean data type.

Integers Literals

The integer literals have a numerous variants. To start with, there are four different number bases allowed for literals: 2, 8, 10, and 16. These correspond to binary, octal, decimal, and hexadecimal numbers. The default specification for numbers is decimal. To specify a binary, an octal, or a hexadecimal number, prepend 0b, 0, or 0x, respectively. For example, the value twenty-eight is specified with a literal as 0b11100, 034, 28, or 0x1c for binary, octal, decimal, or hexadecimal, respectively.

We can insert underscores into an integer literal to make it more readable. For example, we can use underscores in place of commas so that 1_034_000 in code represents the decimal value 1,034,000. Also, we can use underscores to separate bytes in binary representations, like this 0b10010001_11010010. Note that these underscores are ignored by the compiler, but they must not used at the beginning or end of the literal.

We can append to a letter 'L' or 'l' to an integer literal to get the compiler to interpret the value as a long instead of the default, which is int: 7364L.

Floating-Point Literals

The floating point literals have two primary variants. We can use an ordinary decimal representation like this: 201.84. Alternatively, we can use exponents in a manner similar to scientific notation, like this 2.0184e2. The letter e is used to signal a base 10 exponent. We can can use either an uppercase or lowercase e to signal the exponent.

Additionally, we can use hexadecimal floating-point literals with a binary exponent. like this 0x7a.67p7. The letter p is used to signal a base 2 exponent so the literal 0x7a.67p7 has the value 15667.5 in decimal. We can can use either an uppercase or lowercase p to signal the exponent.

Like the integers, we can use underscores to separate digits for readability: 1_243.434_356.

We can append to a letter 'F' or 'f' to a floating-pont literal to get the compiler to interpret the value as a float instead of the default, which is double: 3.14F. We can append a letter 'D' or 'd' to get the compiler to interpret the value as a double; this is redundant unless the literal is an integer value.

Character Literals

The basic single character literal encloses a character in single quotes, like 'a' for the lowercase a character. This can be used for all of the ASCII characters, except a few which must be specified with an escape sequence, like '\n' for the endline character. We can use escape sequences to directly enter ASCII values in octal. Also, we can use universal character representations in hexadecimal, like this '\ub2f4', for representing unicode characters; the numeric value in this case is a four digit hexadecimal number.

String Literals

String literals are similar to single characters, except that they are enclose in double quotes and can contain multiple characters: "XoaX.net". Likewise, we can use the backslash character, \, to generate any character that can be represented by the various escape sequences. Note that all of the characters in a Java string must be written on one line.

Array Literals

In addition to the literals for simpler types, we have literals in Java for arrays. We can create arrays of any type of data. For example, we can initialize an array of three ints with an entry list, like this: {32, 549, -64}. We can then access the entries with the bracket operator and we must remember to use only the index values: 0, 1, and 2.

We can also use nested array entry list literals to intialize multidimensional arrays like this for a two-dimensional 3x2 array of ints: {{3, 2}, {8, 4}, {9, 1}}

 

Escape Sequences

Character SequenceDescription
\bBackspace
\fForm Feed
\rCarriage Return
\nNew Line
\tHorizontal Tab
\'Single Quote
\"Double Quote
\\Backslash
\oooThreee Octal Digit ASCII Value
\uhhhhFour Hexadecimal Digit Unicode (UTF-16) Value
 

Examples


Example 1

// Declare a boolean and initialize it to true.
boolean bIsOver = true;

Example 2

// Initialize four ints to 34, using literals with different number bases.
int iThirtyFourBin = 0b00100010;
int iThirtyFourOct = 042;
int iThirtyFourDec = 34;
int iThirtyFourHex = 0x22;

Example 3

// Initialize doubles with the value of Pi and the charge on an electron.
double dPi = 3.14159;
double dElectronCharge = 1.60217646e-19;

Example 4

// Initialize a double using a hexadecimal value with binary exponent.
// This number has the decimal value of 15667.5.
double dMyDouble = 0x7a.67p7;

Example 5

// Initialize two chars with the same value using a character and an int literal.
char cLowerA = 'a';
char cIntLowerA = 97;

Example 6

// Initialize a char with a universal character literal.
char cExtAscii = '\u00e9';

Example 7

// Initialize a string with a  string literals.
String sName = "XoaX.net";

Example 8

// Initialize a char array with a literal and use it to initialize a String.
char caName[] = { 'X', 'o', 'a', 'X', '.', 'n', 'e', 't' };
String sName = new String(caName);

Example 9

// Initialize an int array with three numbers, using an entry list.
int iaNumber[] = {32, 549, -64};

Example 10

// Initialize a 3x2 two-dimensional int array with nested entry lists.
int iaaNumber[][] = {{3, 2}, {8, 4}, {9, 1}};