Literals

Literals

In this tutorial, we are going to discuss about Literals in java. A constant value that can be assigned to the variable is called “Literal“.

Literals are syntactical representations of fixed value from the source. In Java, Literals are of majorly four types: Character, Boolean, Numeric, and String. So basically, these literals are a bunch of characters that store a constant value for any variable. These literals have immutable characteristics, and we cannot change them.

Literals
int x =20;
int ==> datatype / keyword 
x ==> name of variable / identifier 
20 ==> constant value / literal
Integral Literals

For integral data types (byte, short, int, long) the following are various ways to specify literal value.

1. Decimal literals

Allowed digits are : 0 to 9

E.g

int x = 10;
2. Octal literals

Allowed digits are : 0 to 7

Literal values should be prefix with 0.

E.g

int x = 010;
Hexadecimal literals

Allowed digits are : 0 to 9, a to f or A to F

For the extra digits we can use both upper case and lower case. This is one of few places where Java is not case sensitive. Literal value should be prefixed with 0x or 0X.

E.g

int x = 0x10 or 0X10;

These are the only possible integral literals.

E.g

int x = 10; (Fine)
int x = 0777; (Fine)
int x = 0XFace; (Fine)
int x = 0Xbeef; (Fine)
int x = 0786; (C.E: Integer number too large.. Digit is octal so 8 not allowed)
int x = 0Xbeer; (C.E: Integer number too large.. Digit is hexa decimal so r not allowed)

class Test {
   public static void main(String args[]) {
         int x = 10;
         int y = 010;
         int z = 0X10;
         System.out.println(x + " " +y+ " " +z); // 10 8 16
   }
}

By default every integral literal is of int type but we can specify explicitly as long type by specifying with l or L.

int x = 10;
long l = 10l;
long l = 10 L;
int x = 10 L; // C.E: Possible loss of precision found: long required: int

There is no way to specify integral literal is of byte and short type explicitly. If we are assigning integral literal is with in the range of byte then compiler automatically treats as byte literal. Similarly short literal also.

byte b = 10;
byte b = 130; C.E : Possible loss of precision. found : int required: byte
Floating point literals

Every floating point literal is by default double type and hence we can’t assign directly to float variable. But we can specify explicitly floating point literal to the float variable by suffixing with f or F.

float f = 123.456; //C.E: Possible loss of precision found: double required: float
float f = 123.456f;
double d = 123.456;
double d = 123.456f;

But we can specify explicitly floating point literal as double type by suffixed with d or D. Of course this conversion is not required.

double d = 123.456d;

Note

We can specify floating point literals only in decimal form. Octal and hexa decimal terms are not allowed.

double d = 123.456;
double d  = 0123.456; //Fine. It is not treated as octal it is treated as decimal.
double d = 0X123.456; // Compile time error

We can assign integral literals directly to the floating point variable and that integral literal can be specified either in decimal or octal or hexadecimal form.

double d = 0786; // C.E : integer number is too large
double d = 0XFACE; // Fine
double d = 0777; // Fine
double d = 0XFACE.0; // Compile Error
double d = 0786.0; // Fine

But we can’t assign floating point literals directly to the integer types.

int i = 21.50; // C.E Possible loss of precision found : double required : int

We can specify floating point literal even in exponential form. (Also known as scientific notation)

double d = 1.2e3; // 1.2 * 1000 = 1200;
float f = 1.2e3; // C.E Possible loss of precision found : double required : float
float f = 1.2e3f; // 1200 
Boolean literals

The only allowed values for boolean data type are true or false, where case should be in lower case.

E.g

boolean b = true; //Fine
boolean b = 0; // C.E Possible loss of precision found : int required : boolean
boolean b = True; // Cannot find symbol
boolean b = "true"; // C.E Possible loss of precision found : java.lang.String required : boolean

int x = 0;
if(x) {  // C.E Incompatible types found : int required : boolean
   System.out.println("Hai");
} else {
   System.out.println("Hello");
}

while(1) { // C.E Incompatible types found : int required : boolean
   System.out.println("Hai");
}

int x = 10;
if(x=20) { // C.E Incompatible types found : int required : boolean
  System.out.println("Hai");
} else {
   System.out.println("Hello");
}

int x = 10;
if(x==20) { // Hello
   System.out.println("Hai");
} else {
   System.out.println("Hello");
}
Character literals

A character literal can be represented as single character within the single quotes.

E.g

char ch = 'a'; // Fine
char ch = a ; // C.E: Can't find symbol Symbol : variable a location : class XXXX
char ch = 'ab'; // C.E: Unclosed character literal 'ab' (at a)
                // C.E: Unclosed character literal  'ab' (at b)
                // C.E: not a statement

We can specify character literal as integral literal which represents UNICODE value of that character. The integral literal can be specified either in decimal or octal or hexadecimal forms. The allowed range is 0 to 65535.

E.g.

char ch = 97; // a
char ch = 65535; // ?
char ch = 65536; // Possible loss of precision found : int required : char

A character literal can be represented as in Unicode representation which is nothing but \uxxxx. 4 digit hexa decimal number.

E.g

char ch = '\u0061'; // a
char ch = '\uface'; // ?
char ch = '\iface'; // C.E: invalid escape sequence
char ch = '\Uface'; // C.E: invalid escape sequence
                    // Every escape character in java is a char literal.

E.g

char ch = '\n';
char ch = '\t';
char ch = '\m'; \\ C.E: Illegal escape character

Escape character       Description
    \n              New line character
    \t              Horizontal tab
    \r              Carriage return
    \b              Backspace character
    \f              Form feed
    \'              Single quote
    \"              Double quote
    \\              Back slash
String literals

A sequence of character within ” ” (double quotes) is considered as string literals.

E.g

String s = "Ashok";
Enhancements with respect to literals
Binary Literals

For integral data types (byte, short, int, long) until 1.6 version we can specify literal value in the following ways

  1. decimal literal
  2. octal literal
  3. hexadecimal literal

But from 1.7 version onwards we can specify literal value even in binary form also. The allowed digits are 0 or 1. Literal value should be prefixed with 0b or 0B;

E.g

int i = 0B1111; //15

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

E.g

int num = 111_222;
System.out.println(num); //Prints 111222

double d = 1_23_4.5_67;
System.out.println(d); //Prints 1234.567

We can use more than one _ symbols also between the digits

E.g

double d = 1__2__3.4__5;

We can use _ symbol only between the digits otherwise we will get compile time error.

E.g

double d = _1_23_456.7_8; // C.E
double d = 1_23_456_.7_8; // C.E

That’s all about Literals in Java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java..!!

Literals
Scroll to top