Literals in Java – Free Java Tutorials

Literals -Fundamental of java programming

Literals is any constant value that can be assigned to any variable of any particular data type. It is one of the five tokens of java. The data type specifies the type of literal we use.

For example:

int x = 1;

In the above code

  • int is a data type or keyword.
  • x is a name of variable or identifier.
  • 1 is constant value or ‘literal’.

Types of Literals

Its classification can be of 3 types on the basis of values. Its types are:

Integral Literals in java

For integral data types (byte, short, int, long)we can specify litera-value in the following ways:

  1. Decimal literals: Whenever they are in decimal form, they are called decimal literal. It is the most commonly used approach. Here allowed digits are 0 to 9. For example:int a = 20;

  2. Octal literals: Whenever literal are written in octal form, they are called octal literal. Every literal is prefixed by 0. Here allowed digits are 0 to 7. For example:int a = 020;


  3. Hexadecimal literals: When literal are in hexadecimal number system form, they are represent hexadecimal literal. Every literal here is prefixed by 0X or 0x. Here allowed digits are 0 to 9 and a to f.  Both lower case and upper case characters can be used. For example:int a = 0X20;

  4. Binary literals: When the  literal are in binary number system form, they represent binary literal. For example: int b = 0b1101;

By default every integral literal is of int type. But we can explicitly change it into long type by adding a suffix L.


Floating Point Literals in Java

By default every floating point literal value is of double type. So we cannot assign it directly to float variable. To assign float type literal, a suffix f or F is to be append to the literal. For example:

float b = 12.34f;
double c = 12.34;

Floating point literal can only be specified only in decimal form and not in octal form or hexadecimal form.

We can aslo double literal can with underscore symbol (_) in between the digits. The compiler surely generates an error when the underscore symbol is anywhere other than between of digits . This concept seems to improve the readability of large numbers. For example:

double x = 12_335;

Value of x will be 12335.0


Boolean Literals in java

Possible values for boolean literal are true and false. Unlike other programming languages java does not considers 0 as false and 1 as true. For example:

boolean s = true;


Char Literals in java

We can assign the values to char literal in following ways:

  • Single character within single quotes. For example:char a = 'a';
  • Unicode value of single character. Its allowed range is 0 to 65535. For example:char a = 97;
  • Unicode representation of single character, ‘\uxxxx’, where xxxx is the 4 digit hexadecimal number.For example:char a = '\u0061';

String Literal in java

Any sequence of characters within double quotes (” “) is a string literal. For example:

String str = "Mark";


Possible Errors with Literals

There are chances that literals may throw an error on certain use. It is done due to the incorrect usage of the literal. Its possible cases are:

Integral Literal Errors

byte x = 20;              valid
byte x = 128;            invalid
short y = 32768;      not valid
int z = 128l;             not valid

Floating Point Literal Errors

double f = 12.34;        valid
double f = 0x12.34;    invalid
double f = 012.34;      valid
double f = 0786;           invalid
double f = 0786.0;       valid

Boolean Literal Errors

boolean b = 'true'; invalid
boolean b = true;      valid
boolean b = True;      invalid
boolean b = 1;             invalid

Char Literal Errors

char ch = z;           invalid  by error name ‘Cannot find symbol’
char ch = 'z';      valid
char ch = "z";      invalid  by error name ‘Incompatible Types’
char ch = 'za';    invalid  by error name ‘unclosed character literal’, ‘closed character literal’, ‘not a statement’.

This is all about literals in Java. For any doubt just leave in below comments.

Leave a Comment

Your email address will not be published. Required fields are marked *