Software Training Institute in Chennai with 100% Placements – SLA Institute
⭐ Exclusive Summer Courses Offer ⭐ 💰 Flat ₹5,000 - ₹10,000 off on all courses 👨‍👩‍👧 Additional discounts for group enrollments 🎓 100% Placement Support 🏆 90,000+ Students Successfully Placed 🚀 Avail now! Limited seats only!
Literals in Java
Share on your Social Media

Literals in Java

Published On: April 8, 2026

Introduction to Literals in Java

In the overall architecture of a Java program, literals in Java are the smallest unit of data that a program will use. While a variable is a container for data, a literal is the data that is placed in that container. A literal is a data construct that is used for boolean, integer, character, or string data types. 

In essence, if you have a line of code that says int count = 50;, the literal is the number 50, and the count is the variable identifier. Explore more with our Java tutorial for beginners.

1. Integer Literals

An integer literal is a series of characters that are all digits, without any fractional data. In Java, integer literals are supported in four different number systems.

SystemPrefixAllowed DigitsExample
DecimalNone0-9100
Binary0b or 0B0 and 10b11001
Octal00-7144
Hexadecimal0x or 0X0-9 and A-F0x64

The long Suffix

By default, all integer literals will be assumed to be 32-bit integers. However, if you want to hold a number larger than 231-1, you will need to append an L or as a long (64-bit).

int hexVal = 0x1a;       // 26 in decimal

int binVal = 0b11010;    // 26 in decimal

long bigVal = 99999999L; // Long literal

2. Floating Point Literals

These are used for representing numbers with decimal points. They can be represented in standard decimal form or scientific notation using “e” or “E”.

  • Double Literals: The default literal type. It can be optionally written using “D” or “d”.
  • Float Literals: The “f” or “F” suffix is required. Failure to do so will result in a compilation error because of “lossy conversion.”

Code Example:

double pi = 3.14159;

double scientific = 1.23e4; // 12300.0

float price = 19.99f;       // Required ‘f’

Learn with complete hands-on exposure in our software training institute in Chennai.

3. Character Literals

A character literal is represented as a single character enclosed in single quotes. The single quotes can contain any character. Java supports Unicode character sets, so we can use almost any language’s character set.

Special Representations

  • Single Quote: ‘a’, ‘$’, ‘7’
  • Unicode: ‘\u0061’ (the letter ‘a’)
  • Escape Sequences: Used for characters that cannot be typed directly.
Escape SequenceDescription
\nNew line
\tTab space
\bBackspace
\’Single quote
\”Double quote
\\Backslash

4. String Literals

A string literal is a series of characters enclosed between two double quote marks (” “). Unlike other programming languages, Java does not allow strings and characters to be used interchangeably. It does not allow single quote marks for strings.

String Pool Concept

Java uses a special technique to store string literals. This technique is known as the String Constant Pool. Suppose we write a program: String s = “Hello”;. Then Java checks if “Hello” is present in a special memory location.

Code Example:

String greeting = “Hello World!”;

String path = “C:\\Users\\Desktop”; // Using escape characters

Practice more with our Java project ideas.

5. Boolean and Null Literals

This is the simplest form of literals used in Java.

  • Boolean Literals: Only two boolean literals exist: true and false. They don’t relate to any numbers (in contrast to C/C++, where 1 and 0 are used).
  • Null Literals: This is indicated by the keyword null. This keyword is used for reference variables that don’t point to any object.

6. Underscores in Numeric Literals

Java provides a feature that allows you to make your code easier to read by putting an underscore between any two digits in a numeric literal. This feature is not used by the compiler.

Rules for Using Underscores:

  • It can’t be at the start or end of a number.
  • Not allowed immediately after a decimal point.
  • Not allowed immediately before an L or F.

Code Example:

long creditCardNumber = 1234_5678_9012_3456L;

float bytes = 0b1101_0101_1100_0010f;

double socialSecurity = 999_00_1234;

7. Class Level View: Literals and Constants

A constant is a variable declared with final and holding a literal.

// ’10’ is the literal

// ‘MAX_USERS’ is the constant

final int MAX_USERS = 10;

Literal CategoryExamplesDefault Type
Integer10, 0x7B, 0b101int
Floating Point10.5, 12.3f, 4.5e2double
CharacterA’, ‘\u0041’, ‘\n’char
String“Java”, “123”, “”String
Booleantrue, falseboolean

Conclusion

The literals need to be understood for memory management and to avoid precision errors, especially for float and double. By using underscores and prefixes, you make your code easier to maintain and avoid confusion with magic numbers. Learn further with our comprehensive Java training in Chennai.

Share on your Social Media
Get Your Instant Job & Placement Eligibility
Report in Just 30 Seconds!
Below 30% - not Eligible (Needs Preparation)
30% – 70% - Partially Eligible (Needs Guidance)
Above 70% - Fully Eligible (Ready to Start)

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.