Literals in Java

In Java, we give a fixed value in a source code as literals. These are nothing but notations and are generally known as tokens. this article will let you dive deep into the concept of literals in java and more about it.

What are literals?

Literals are fixed or constant values that are present in a program. We usually assign these values to a variable. It can be a representation of a boolean expression, numeric, character, or string data.

int num = 23;

Here int is the data type and 23 is the literal.

Types of Literals in Java:

In Java, literals are classified into six main types:

  • Integer Literal
  • Floating Literal
  • Character Literal
  • Boolean Literal
  • String Literal
  • Null Literal`

1. Java Integer literals:

In integral data type, we can denote the literal in four ways, byte, short, int, and long.

i. Decimal Integer:

These integers have a base value of 10 and include the digits present between 0 to 9. Without points, it can either be a positive(+) or negative(-) value.

For example: 500, 72, -23, etc.

int decimal_value = 65;

ii. Octal Integer:

These integers have a base value of 8. It includes the values present between 0 to 7. All the octal numbers begin with a ‘0’. For example: 014, 023, 033, etc.

int octal_value = 035;

iii. Hexadecimal Integer:

These integers have a base value of 16. It includes the values present between 0 to 15. Whereas, the values 10 to 15 are denoted by letters a to f. Therefore, the integers that begin with 0x or 0X are hexadecimal integers. For example: 0xff, 0xf1f2, etc.

int hexadic_value = 0x1ff2;

iv. Binary Integer:

These integers have the base value 2. It contains only two digits, zeros (0) and ones(1).

int binary_value = 0101010101;

Sample program to denote all the literals:

public class FirstCode_Literals
{
public void main()
{
int decimal_value=55;
int octal_value=077;
int hexadec_value=0x1ff2;
int binary_value=0b1010101;
System.out.println("This is a Decimal Literal: "+decimal_value);
System.out.println("This is an Octal Literal: "+octal_value);
System.out.println("This is a Hexa Decimal Literal: "+hexadec_value);
System.out.println("This is a Binary Literal: "+binary_value);
}
}

Output:

This is a Decimal Literal: 55
This is an Octal Literal: 63
This is a Hexa Decimal Literal: 8178
This is a Binary Literal: 85

2. Java Floating Point Literal:

The floating-point literals contain decimal points between them. They usually belong to the double data type by default. We can assign float data type values by adding an f at the end of the value.

Example of declaring a float literal:

float val_float = 1.234f;

Example of declaring a double literal:

float val_double = 1.566; // The compiler assigns double datatype in a default manner.
float val_double = 1.5666d;
double val_double = 1.5666;

We can also include an exponential part in the floating-point literals. Here is an example of how to declare them:
145E-21f

Notes to keep in mind while declaring floating-point literals in Java:

1. If the suffix is not present, the default type is double.
2. F or f suffix denotes a floating data type.
3. D or d suffix denotes a double data type.

Legal and Illegal Floating literals:

Here are a few legal and illegal floating literals:
543.12 //Legal
62.21E5 //Legal
121.16F //Legal
1/2 //Illegal Symbol Used “/”
3.1.7 //Illegal, as two decimal points are present.
1,56.78 //Illegal, as the comma is present
42.E2 //Illegal, as E cannot precede the point.
451E //Illegal, as the exponent part, is incomplete.

Sample code to implement java float literals:

public class FirstCode_LiteralsFloat
{
public void main()
{
float val_float=1.6532f;
double val_double=1.453d;
float val_exponent=45E4f;
System.out.println("This is a Floating Point Literal: "+val_float);
System.out.println("This is a Decimal Literal: "+val_double);
System.out.println("This is an Exponential Literal: "+val_exponent);
}
}

Output:

This is a Floating Point Literal: 1.6532
This is a Decimal Literal: 1.435
This is an Exponential Literal: 450000.0

3. Java Boolean Literals:

Boolean literals contain two values, true and false. We declare these literals using the keyword boolean. We usually use this to declare flag variables in various programs to terminate a looping sequence.

Sample program to implement java boolean literals:

public class FirstCode_LiteralsBoolean
{
public void main()
{       
boolean flag1=true;
boolean flag2=false;
System.out.println("This is a boolean true flag variable: "+flag1);
System.out.println("This is a boolean false flag variable: "+flag2);
}
}

Output:

This is a boolean true flag variable: true
This is a boolean false flag variable: false

4. Java String Literals:

A string is an array of characters. In Java, there is a separate class for strings. Using this class, we can implement various operations effortlessly. If you are perplexed about knowing what exactly is a string, it is nothing but a set of characters written inside double-quotes.

Example of String Literal in java:

String name = “FirstCode”; //Valid String literal
String name = FirstCode; //Invalid String as there as no double-quotes.

5. Java Null Literals:

We can also declare void strings in Java with the help of a special literal known as null literal. Basically, this is equivalent to the integer value 0.

Sample program to implement null literal:

public class FirstCode_LiteralsString
{
public void main()
{
String name="FirstCode";
String null_Literal=null;
System.out.println("This is a String Literal: "+name);
System.out.println("This is a null Literal: "+null_Literal);
}
}

Output:

This is a String Literal: FirstCode
This is a null Literal: null

6. Java Character Literals:

The Character Literals are denoted using single quotes. The main difference between a string literal and a character literal is that the former consists of many characters and the latter contains only one character.

We can represent the character literal in four ways:]

1. Single quote character:

Example: char ch = ‘F’;

2. Character Literal as an Integer literal:

Example: char number = 0123;

3. Unicode representation as an Integer Literal:

char unic = ‘\u0023’;

4. Escape Sequence:

These are special literals that are preceded by a backslash ‘\’. They contain a unique feature where most of them are used as escape sequence characters.

Here is a list of the prime escape sequences:

Escape Sequence Functionality
\0 Null Character
\0n Used to represent an octal number
\a Used to add a small beep sound.  
\b Used to insert a blank space.
\f Used to represent formfeed
\n Used to insert a new line.
\r Used for carriage return.
\t Used to insert a horizontal tab.
\uHn Used to represent Unicode CharacterThrough its hex code.
\v Used to insert a Vertical tab.
\xHn Used to represent Hexadecimal number
\’ Used to add a single quote inside a string.
\” Used to add double quotes inside a String.
\? Used to add a Question mark
\\ Used to add a backslash

Other Valid Character Literals:
char ch = ‘A’;
char ch =’\u0065’
char ch= 0065
char ch=’\\’
char ch=’%’

Program to implement java character literal:

public class FirstCode_LiteralChar
{
public void main()
{
char ch='A';
char number= 0065;
char uni='\u0065';
System.out.print("\nThis is a character Literal: \b"+ch);
System.out.print("\nThis is a character Literal as Integer: \b"+number);
System.out.print("\nThis is a character Literal as Unicode: \b"+uni);
}
}

Output:

This is a character Literal: A
This is a character Literal as Integer: 5
This is a character Literal as Unicode: e

Special literal – Class Literals:

Another special literal is the class literal in Java. To form a class literal, we must append a type name and “.class” to it. For example, String.class is a class literal.

Class<String> c = String.class;

Things to note while using underscores in literals:

  • We cannot add underscores in any position of a literal. There are certain rules to follow.
  • In the first, end, or in between the numbers.
  • Preceding to an F or L suffix.
  • Next to a decimal point in a floating-point literal.
  • In places where a string of digits will occur.

Invalid uses of underscores:

Here are a few samples of how we should not use underscores.
1_.432
1._674
21_
12_34_56_78_90_L
0_x44
0x_54
0x21_

Uses of Literals in Java:

In the presence of literals, we can reduce the declaration of constants and add labels. This takes place in a single line with literals.

How to use literals in java?

We normally assign any value to a variable name. this variable name usually contains a data type. The ‘=’ sign assigns a value of the literal to the variable.

Conclusion:

Though there are six different types of literals in java, the null literal is the least used one. We usually use the other five literals in many areas of our java code. I hope this article would have helped you to understand Java literals in depth. now, you can easily use them in your program codes without any misperceptions.

Leave a Reply

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