Literals

1. Numeric Literals

1.1 Integer Literals

Types of integer literals in Java language:

  • decimal (base 10)
  • octal (base 8)
  • hexadecimal (base 16)
  • binary (base 2) - starting from Java 7

All integer literals are defined as int by default, but they may be specified as long by placing a suffix of L or l at the end:

Example 1.1 Integer Literals defined as long

long a1 = 220433L;
long a2 = 0x3FFl;

1.1.1. Decimal Literals

Example 1.2. Definition of Decimal Literal

int a = 878;

1.1.2. Octal Literals

Octal integers use the digits from 0 to 7. The Java octal literal is represented in octal form by placing a zero in front of the number, for example:

Example 1.3. Definition of Octal Literals

int six = 06; // Equal to decimal 6
int seven = 07; // Equal to decimal 7
int eight = 010; // Equal to decimal 8
int nine = 011; // Equal to decimal 9

1.1.3. Hexadecimal Literals

  • Hexadecimal numbers are built using 16 distinct symbols: 0 1 2 3 4 5 6 7 8 9 a b c d e f
  • The letters a b c d e f can be capital or lowercase.
  • Hexadecimal literals should start with 0x or 0X.
  • It is allowed up to 16 digits in a hexadecimal number, not including the prefix 0x or the optional suffix extension L.

For example:

Example 1.4. Definition of Hexadecimal Literals

int a1 = 0X0005;
int a2 = 0x7ddfffff;
int a3 = 0xcccccccc;

1.1.4. Binary Literals in Java 7

To specify a binary literal, a prefix 0b or 0B should be added to the number. For example:

Example 1.5. Definition of Binary Literals

int i1 = 0b101;
int i2 = 0B101; 

1.2. Floating-Point Literals

Example 1.6. Definition of Floating-Point Literal

double d1 = 145454545.676767676;

Floating-point literals are defined as double (64 bits) by default.

To assign a floating-point literal to a variable of type float (32 bits), attach the suffix F or f to the number.

For example:

Example 1.7. Assigning a Floating-Point Literal to a Float Type

float f1 = 56.45455; // Compiler error
float f2 = 343434.454563F; 
float f3 = 78.45f; 

It is possible to attach a D or d to double literals, but it is not required:

Example 1.8. Attaching "D" to a Double Literal

double d1 = 454545.454545D; 
double d2 = 654.321;

1.3. Underscores in Numeric Literals

From Java 7, it is possible to place any number of underscores between the digits of numbers to separate groups of digits and improve readability.

It is prohibited to put underscores in the following places:

  • At the beginning or end of a number
  • Adjacent to a decimal point in a floating-point literal
  • Before an F or L suffix
  • In positions where a string of digits is expected

Example 1.9. Using Underscores in Numeric Literals

int i1 = 12_567;            // OK
double d1 = 56.356_234;      // OK
short s = 0b000____0100;    // OK
int i2 = 220_;              // Compilation error
int i3 = 0_x2F;             // Compilation error
int i4 = 0x_3E;             // Compilation error
float f1 = 6345.56_f;       // Compilation error
float f2 = 3423_.87f;       // Compilation error

2. Boolean Literals

A boolean value can be defined as true or false:

Example 2.1. Definition of Boolean Literals

boolean b1 = true; // Legal
boolean b2 = 0; // Not legal

3. Character Literals

A character literal can be represented by a single character in single quotes:

Example 3.1. Definition of Character Literals by a Single Character

char c1 = 'n';
char c2 = '#';

It can be represented by the Unicode value of the character, using the Unicode notation of prefixing the value with \u or \:

Example 3.2. Definition of Character Literals by Using the Unicode Value

char letterO = '\u004F'; // The letter 'O'
char letter_a = '\141';  // The letter 'a'

It is possible to assign a number literal, which fits into the unsigned 16-bit range (0-65535). For example:

Example 3.3. Assigning a Number Literal to char type

char a1 = 0x675; // hexadecimal literal
char a2 = 345; // int literal
char a3 = (char)80000; // The cast is done, because 80000 is
// out of char range

An escape code can be used to represent a character that can't be typed in as a literal, for example, the characters for newline, linefeed, horizontal tab,

backspace, double and single quotes:

Example 3.4. Definition of Character Literals with an Escape Code

char c1 = '\''; 
char c2 = '\n';
Trustpilot
Trustpilot
Comments