×

Literals in Java

In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them.

Literal

Literal refers to any constant value that can be used as a variable value.

Literals are an artificial description of Boolean, integer, character, and string data in Java, to put it simply. It is a way for the code to express specific values. The permanent values that are introduced directly into a code are known as literals. It can be automatically assigned to a variable.

There is no need for or usage of calculation because the literals are expressed explicitly in the code. Additionally, because they may be allocated to any primitive form of variable, they are used to optimize the process and make it easier to complete the task.

Types of Literals

Java literals could be divided into six categories.

  • Integral Literals
  • Floating-point Literals
  • Char Literals
  • String Literals
  • Boolean Literals

Integral Literals

Sequences of integers make up integer literals. Integer literals are divided into four distinct categories. They are:

Decimal Literals

Its base is ten. The permitted digits in this type are 0 to 9.

For example: int q= 123;

Octa Literals

It features an eight-base and accepts digits between 0 and 7. The prefix 0 is required when using an octal literal for Java programming.

For example: int q= 0123;

Hexadecimal Literals

It has a base 16. Hexadecimal supports letters from A to F and numbers from 0 to 9. Hexadecimal literals can be written in either capital or lowercase letters, despite the fact that Java is case sensitive.  The hexadecimal number must be represented with 0X or 0x mandatorily.

For example:  int q = 0x123;

Binary Literals

Beginning with version 1.7, the only permitted digits for a literal value are 0 and 1. The prefix 0b or 0B must be used before literal values.

For example: int q = 0b1001;

Lets understand along with an example program

File name: IntegralL.java

// A Java application that demonstrates the use of integer literals
public class IntegralL {
public static void main(String[] args)
{
// decimal literal
int q = 123;
// octal literal
int r = 0123;
// Hexadecimal literal
int s = 0x123;
// Binary literal
int t = 0b1001;

System.out.println(q);
System.out.println(r);
System.out.println(s);
System.out.println(t);
}
}

Output

123
83
291
9

Floating-point Literals

For Floating-point data types, we can specify literals in only decimal form, and we cant specify in octal and Hexadecimal forms. 

With a leading + or - sign, floating-point literals can denote either a positive or negative number. If nothing else is provided, the value will always be received positively. The use of floating-point literals makes it easier to provide values based on instance needs. For instance, it offers values that may be applied to either float or double cases. It is important to distinguish between integer & floating-point literals since the integer literals have definite integer values while the floating-point literals have either fractional or decimal values instead.

For example: int w= 123.456

File name: FloatL.java

// A  java program to demonstrate the use of floating-point literals
public class FloatL {
public static void main(String[] args)
{
// decimal literal
float x = 135.023;
// It also a decimal literal
float y = 0145.289;

                          System.out.println(x);
System.out.println(y);
  }
}

Output

135.023
145.289

Char Literals

Character (Char) literals are arbitrary integer primitive types with the type char. In the Java code, they are character statements with fixed values. The frequency of these sixteen-bit Unicode characters is 0 to 65535. In Java, char literals are written as the character in a single-quote, one beginning quote, and one concluding quote.

For example: char ch = k;

char ch = ‘\U+006B’

File name: CharL.java

// A simple java program to demonstrate the use of char literals
public class CharL {
public static void main(String[] args)
{
// This is a single character in single quotes
char ch= ‘k’;
// Its Unicode representation
char w=’\u006B’;

                          System.out.println(ch);
System.out.println(w);
  }
}

Output

k
k

String Literals

String literals are a series of (zero or more, containing Unicode characters) alphabets or characters enclosed in double quotes. The Java compiler automatically evaluates statements that were defined at compile time, even though string literals may not contain escape line break or newline characters. Only within string as well as character literal, special characters can be escaped using Unicode break sequencing or backlash characters.

For example: String z= “Hello again”;

File name: StringL.java

// Java program that demonstrates the implementation of String literals
public class StringL {
public static void main(String[] args)
{
String f = " Hey, Jimmy";
                          System.out.println(f);
}
}

Output

 Hey, Jimmy

Boolean Literals

Boolean literals are split into two categories because they only support two values.

True: Denotes a real boolean value.

False: Denotes a unreal boolean value.

The logical value is represented by the boolean literals as either true or false. These parameters are not case-sensitive; they are still acceptable whether they are written in uppercase or lowercase.

For example: boolean h= false;

// Java program that demonstrates the implementation of boolean literals
public class BooleanL {
public static void main(String[] args)
{
boolean h = true;
boolean g = false;
System.out.println(h);
System.out.println(g);

}
}

Output

true
false

Related Topics

Java Binary to Hexadecimal

Converting between types in programming is an important task. Moving from one kind to another kind conversion is occasionally necessary. We have discussed numerous conversion types in the section on...

3 minutes read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Three Partition Problems in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

8 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

Java URL Class with Example

Java URL Uniform Resource Locator To find any resource on the internet, you need to have an address of it. The URL and IP addresses are the pointers used for this purpose....

12 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

Minimum Difference Between Groups of Size Two in Java

There is given an array with various integers in it. The goal is to divide the elements into distinct groups, each of which has just two, so that the difference...

3 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.