Java : Types of Literals

A literal is the source code representation of a fixed value and assigned directly to variable without requiring computation.

Literal assigned to primitive type variable as it’s not required new keyword. Primitive types are special data types built into the language; they are not objects created from a class.

Example of Literals:

boolean result 	= false;
char capitalM 	= 'M';
byte b 			= 200;
short s 		= 20000;
int i 			= 200000;

Type of Literals:

Java supports these types of Literals as below:

Java : Literal Types
Java : Types of Literals

Integer Literals

All integer values for primitive type byte, short, int and long are comes under Integer Literal. Below are some points that need to keep in mind while handling Integer Literals:

  • An integer literal is of type long if it ends with the letter L or l (Recommend use L); otherwise it is of type int.
  • Values of the integral types byte, short, int, and long can be created from int literals. Values of type long that exceed the range of int can be created from long literals.
  • Integer literals can be expressed by these number systems:
    • Decimal:Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day.
    • Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F. Prefix 0x represent hexadecimal.
    • Binary: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later) .Prefix 0b represent Binary system.

Below are diffrent representation of decimal integer value 26:

Decimal :

int decVal = 26;

Hexadecimal :

int hexVal = 0x1a;

Binary :

int binVal = 0b11010;

Floating Literals :

All floating values for primitive type float and double comes under Floating Literals. Below are some points that need to keep in mind while handling Floating Literals:

    • Floating points literals for float ends with the letter F or f. (32-bit float literal)
    • Floating points literals for double ends with the letter D or d(Optional) (64-bit double literal; this is the default and by convention is omitted)
    • Floating points literals for float and double can also be expressed using E or e (for scientific notation).

A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.

Below are diffrent representation of decimal value 123.4:

Decimal :

double d1 = 123.4;

Decimal Scientific Notation :

double d2 = 1.234e2;

Floating :

float f1 = 123.4f;

 

Character and String Literals

Literals of types char and String may contain any Unicode (UTF-16) characters. Always use ‘single quotes’ for char literals and “double quotes” for String literals.

Java also support few special escape sequences for char and String literals:

\b (backspace)
\t (tab)
\n (line feed)
\f (form feed)
\r (carriage return)
\” (double quote)
\’ (single quote)
\ (backslash).

Note: If your editor and file system allow it, you can use such characters directly in your code. If not, you can use a “Unicode escape” such as ‘\u0108’ (capital C with circumflex), or “S\u00ED Se\u00F1or” (Sí Señor in Spanish).

Note :Unicode escape sequences may be used elsewhere in a program , not just in char or String literals.

Boolean Literals

A Boolean Literals is of type boolean which allow values as true or false.
For Example:

boolean isValidUser=false;
boolean isPetrolCar=true;

Null Literals

null literal is often used in programs as a marker to indicate that refrence type object is unavailable. null may be assigned to any variable, except variables of primitive types.
For Example:

String employeeName=null;
Employee employee=null;

Class Literals

Class literal formed by taking a type name and appending “.class”; for example, String.class. This refers to the object (of type Class) that represents the type itself.

For Example:

class classType=String.class;

Underscore Characters in Numeric Literals (java 7+)

Any number of underscore characters (_) can appear anywhere between digits in a numerical literal. After java 7 this feature enables to separate groups of digits in numeric literals, which can improve the readability of your code.
It’s similar to use an underscore character to separate digits in groups of three, as we use a punctuation mark like a comma, or a space, as a separator.

Restrictions to use underscore (_):

      • Underscore can be used between digits.
      • At the beginning or end of a number.
      • Adjacent to a decimal point in a floating point literal.
      • Prior to an F or L suffix
      • In positions where a string of digits is expected.

Valid Numeric Literals

long creditCardNumber 		= 1234_5678_9012_3456L;
long socialSecurityNumber 	= 999_99_9999L;
float pi 					=  3.14_15F;
long hexBytes 				= 0xFF_EC_DE_5E;
long hexWords 				= 0xCAFE_BABE;
long maxLong 				= 0x7fff_ffff_ffff_ffffL;
byte nybbles 				= 0b0010_0101;
long bytes 					= 0b11010010_01101001_10010100_10010010;
int x1 						= 5_2;
int x3 						= 5_______2;
int x6 						= 0x5_2;

Invalid Numeric Literals

    • Underscore adjacent to decimal point
      float pi1 = 3_.1415F;
      float pi2 = 3._1415F;
      
    • Underscore prior to an L suffix
      long socialSecurityNumber = 999_99_9999_L;
      
    • Underscore at the end of Literals
      int x2 = 52_;
      
    • Underscore in the 0x radix prefix
      int x4 = 0_x52;
      
    • Underscores at the beginning of a number
      int x5 = 0x_52;
      
    • Underscores at the end of a number
      int x7 = 0x52_;