Programmers have a vast selection of tools at their disposal. The demand for more and better software, applications, and websites is on the rise, so programmers need to bring every helpful resource to bear to promptly create a reliable, excellent product. The need for more products drives the desire for ways to optimize programming for faster outputs, memory, and efficiency.

In Java, literals are constant values that are allocated to numeric or textual variables. They represent fixed values and are essential building blocks for developing Java programs. Programming in Java requires an understanding of literals. Learn more about literals and other fundamental Java concepts in a Java course to improve your foundational knowledge of programming.

Today, we're looking at one element of Java programming called literals. This article demystifies literals, exploring what it is, what types exist, and how to use them, offering a few examples, and addressing the most asked questions.

Let’s begin with a definition.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What Are Literals in Java?

Variables are critical in programming because that’s how you store data in a particular memory location. For example, a Java program, while running, stores values in containers known as “variables,” which are defined as a basic storage unit. To enhance the program’s readability, the programmer must follow particular conventions while naming these variables and assigning values to them. For example, a source code representing a fixed value is "literal.”

Literals in Java are a synthetic representation of boolean, character, numeric, or string data. They are a means of expressing particular values within a program. They are constant values that directly appear in a program and can be assigned now to a variable. For example, here is an integer variable named ‘’/count assigned as an integer value in this statement:

int count = 0;

“int count” is the integer variable, and a literal ‘0’ represents the value of zero.

Therefore, a constant value that is assigned to the variable can be called a literal.

Types of Literals in Java

Literals in Java are typically classified into six types and then into various sub-types. The primary literal types are:

1. Integral Literals

Integral literals consist of digit sequences and are broken down into these sub-types:

  • Decimal Integer: Decimal integers use a base ten and digits ranging from 0 to 9. They can have a negative (-) or a positive (+), but non-digit characters or commas aren’t allowed between characters. Example: 2022, +42, -68.
  • Octal Integer: Octal integers use a base eight and digits ranging from 0 to 7. Octal integers always begin with a “0.” Example: 007, 0295.
  • Hexa-Decimal: Hexa-decimal integers work with a base 16 and use digits from 0 to 9 and the characters of A through F. The characters are case-sensitive and represent a 10 to 15 numerical range. Example: 0xf, 0xe.
  • Binary Integer: Binary integers uses a base two, consisting of the digits “0” and “1.” The prefix “0b” represents the Binary system. Example: 0b11011.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

2. Floating-Point Literals

Floating-point literals are expressed as exponential notations or as decimal fractions. They can represent either a positive or negative value, but if it’s not specified, the value defaults to positive. Floating-point literals come in these formats:

  • Floating: Floating format single precision (4 bytes) end with an “f” or “F.” Example: 4f. Floating format double precision (8 bytes) end with a “d” or “D.”
    Example: 3.14d.
  • Decimal: This format uses 0 through 9 and can have either a suffix or an exponent. Example: 99638.440.
  • Decimal in Exponent form: The exponent form may use an optional sign, such as a "-," and an exponent indicator, such as "e" or "E." Example: 456.5f.

3. Char Literals

Character (Char) literals are expressed as an escape sequence or a character, enclosed in single quote marks, and always a type of character in Java. Char literals are sixteen-bit Unicode characters ranging from 0 to 65535. Example: char ch = 077.

4. String Literals

String literals are sequences of characters enclosed between double quote ("") marks. These characters can be alphanumeric, special characters, blank spaces, etc.

Examples: "John", "2468", "\n", etc.

5. Boolean Literals

Boolean literals have only two values and so are divided into two literals:

  • True represents a real boolean value
  • False represents a false boolean value

So, Boolean literals represent the logical value of either true or false. These values aren't case-sensitive and are equally valid if rendered in uppercase or lowercase mode. Boolean literals can also use the values of “0” and “1.”


boolean b = true;

boolean d = false;

6. Null Literals

Null literals represent a null value and refer to no object. Nulls are typically used as a marker to indicate that a reference type object isn’t available. They often describe an uninitialized state in the program. It is a mistake to try to dereference a null value. Example: Patient age = NULL;

Remember, not everyone divides literals in Java into these six types. Alternative classifications split literals into as few as four types (Integer, Character, Boolean, and String) or as many as ten (Integer, Real, Backslash, Character, String, Floating-Point, Boolean, NULL, Class, and Invalid).

How to Use Literals

Programmers who want to incorporate literals in their program begin with the prefix “=,” followed by the specific value.

A Java Literal Example

Here’s a sample of Java programming showing literals:

public class LiteralsExample  


public static void main(String args[])   


int count = 987;  

float floatVal = 4534.99f;  

double cost = 19765.567;  

int hexaVal = 0x7e4;  

int binary = 0b11010;  

char alpha = 'p';  

String str = "Java";  

boolean boolVal = true;  

int octalVal = 067;  

String stuName = null;  

char ch1 = '\u0021';  

char ch2 = 1456;  












System.out.println("\t" +"backslash literal");  
















     backslash literal



Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Frequently Asked Questions for Literals in Java

1. Can I get a simple, succinct definition of literals in Java?

Sure. Literals are constant values assigned to constant variables. Literals represent fixed values that cannot be modified. Literals are a synthetic representation of boolean, character, numeric, or string data, a medium of expressing particular values in the program.

2. What are literals used for in Java?

Literals are source code representations of a fixed value, represented directly in the code, without any computation.

3. So why should I use literals in Java?

To avoid having to define a constant somewhere and create a label for it. Instead, all you must do is write the value of a constant operand as part of the instructions. Literals are more memory efficient.

4. What are the different data types in Java?

Some basic Java data types include: Floating Point (double), Integer (int), Character (char), and Boolean. Java also uses wrapper classes to support primitive data types. These classes are Byte, Long, Float, Short, Integer, and Double.

5. Can literals in Java ever be changed?

No. Literals consist of immutable characteristics and thus cannot be changed. When programmers create a new variable or a constant, they define a data type and assign particular values. So, when the compiler reads a constant variable's value, it reads and interprets the values as Literals.

6. Why are strings immutable in Java?

Strings are immutable because it is a valuable property for programmers to work with. Immutable objects are easier to reason than their mutable counterparts, and copying the former is fast and easy. Also, immutable objects don't waste memory since the memory isn't changed when the strings are passed around. However, when you pass around mutable objects, their data can be modified by other objects in their path, which eats memory. Hence, choosing immutable objects over mutable ones at every opportunity would be best.

Get access and complete hands-on experience on a plethora of software development skills in our unique Job Guarantee bootcamp. Get job-ready with HackerEarth and HIRIST by enrolling in our comprehensive Full Stack Java Developer Masters program today!

Now You Can Become a Full Stack Developer!

Full stack developers are in greater demand than back or front-stack developers because they have a complete skillset that lets them tackle any application or webpage development aspect. In addition, businesses need developers to keep up with the greater demands for software and web pages.

If you like the notion of becoming a full-stack developer, Simplilearn has the training tools to get you started. The Full Stack Java Developer Master’s program can advance your career as a full-stack developer. You will learn vital skills in demand by the IT industry, including Angular, Spring Boot, Hibernate, Servlets, and JSPs, in addition to MVC, web services, and SOA to help you build highly scalable web apps.

This program is designed to give you a solid understanding of front-end, middleware, and back-end Java web developer technologies. You will learn how to construct end-to-end applications, test and deploy code, store data using MongoDB, and much more.

Indeed reports that a full stack web developer in the United States can make an annual average of $101,402. So stop by Simplilearn today and get your start on a rewarding, challenging, in-demand career!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 5 Aug, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 30 Jul, 2024

6 Months$ 1,449
Full Stack Developer - MERN Stack

Cohort Starts: 30 Jul, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 7 Aug, 2024

11 Months$ 1,499