Java Literals Tutorial W3Schools Guide Examples

Robert Jul 13, 2026

Java literals are fundamental to programming, serving as the initial values for various data types when declared. W3Schools, a renowned web development learning resource, provides an excellent guide on Java literals to help one grasp their significance and usage. Let's explore Java literals through this lens.

two different types of words that appear to be in the same language, one is for each
two different types of words that appear to be in the same language, one is for each

Java literals allow us to represent fixed values in our code. They come into play during the declaration of variables and constants. Understanding Java literals enables us to write meaningful and efficient code.

java at a glance 😎
java at a glance 😎

Numeric Literals

Numeric literals represent numerical values without anyUnits. They can be of different types like integers, floating-point numbers, or in hexadecimal and octal forms.

a table with different types of words and numbers on it, including names for each word
a table with different types of words and numbers on it, including names for each word

To represent decimal numbers, you just enter the numerical value, like 123. For floating-point numbers, you can use the decimal point, like 345.6.

Integer Literals

Reading and writing data using file input stream and output stream class
Reading and writing data using file input stream and output stream class

Integer literals represent whole numbers. They can be written as decimal (for example, 175), hexadecimal (for instance, 0xFF), or octal (like 0257) values.

To distinguish between an octal and hexadecimal literal, you use a prefix. An octal literal starts with a zero, while a hexadecimal literal starts with 0x.

Floating-Point Literals

Java in 2024: Still Brewing Strong — Why You Should Learn It
Java in 2024: Still Brewing Strong — Why You Should Learn It

Floating-point literals represent rational numbers. They are written with a decimal point, like 122.345, or in scientific notation with an 'e' or 'E' followed by the power of 10, like 1.22E-2.

These literals are useful when dealing with numerical data that has a fractional component, such as measurements or mathematical results.

Textual Literals

Java Bhobdhama
Java Bhobdhama

Textual literals represent sequence of characters, enclosed within double or single quotes. They are mainly used for strings of text, like "Hello, World!" or 'This is a string'.

The primary difference between the two lies in escape characters. Certain characters, such as the newline or tab, can be represented using escape sequences like \n or \t, and the character '\"' can be represented by \" within double quotes and '\\' within single quotes.

a diagram with several different types of words
a diagram with several different types of words
Hashset initialization java - HashSet Class in Java with Example | Learn What is HashSet in Java & How to get started with it - BTech Geeks
Hashset initialization java - HashSet Class in Java with Example | Learn What is HashSet in Java & How to get started with it - BTech Geeks
Type Conversion and Casting in Java - Scientech Easy
Type Conversion and Casting in Java - Scientech Easy
☕ A Evolução do Java: Menos código, mais resultado! 🚀
☕ A Evolução do Java: Menos código, mais resultado! 🚀
Deep Dive into java numbers-  Methods with syntax and examples
Deep Dive into java numbers- Methods with syntax and examples
a poster with different types of text and symbols on the back ground, including an image of
a poster with different types of text and symbols on the back ground, including an image of
Core_Java_Fundamentals
Core_Java_Fundamentals
Polymorphism in Java | Definition and How to Use it in the Code
Polymorphism in Java | Definition and How to Use it in the Code
30 Days of Java – Day 2 | Variables & Data Types Explained for Beginners and students
30 Days of Java – Day 2 | Variables & Data Types Explained for Beginners and students
Java While Loop Explained 🔁 | Easy Guide for Beginners
Java While Loop Explained 🔁 | Easy Guide for Beginners
a black background with text that reads, dynamic programming and other things to know about it
a black background with text that reads, dynamic programming and other things to know about it
java
java
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Java mini projects with source code and output
Java mini projects with source code and output
JAVA
JAVA
Classes and Objects in Java - Scientech Easy
Classes and Objects in Java - Scientech Easy
java
java
🔥 Java try-catch Explained! 🚀
🔥 Java try-catch Explained! 🚀
Welcome to Java Programming for Kids: Visual Lessons and Projects
Welcome to Java Programming for Kids: Visual Lessons and Projects
What is JAVA?
What is JAVA?

String Literals

String literals are used to represent text data. They are enclosed within double or single quotes. The use of double quotes allows for escape characters like \" or \n.

You can also use the StringBuilder or StringBuffer classes in Java to manipulate string data and create multi-line strings.

Character Literals

A single character can be represented as a constant using a character literal. These are enclosed within single quotes and can hold a value from the Unicode character set.

If a character literal is a one, two, or three-digit Unicode character, it can also be represented using octal or hexadecimal values. For example, '\727' represents the seventh octal digit, while '\x4A' represents the hexadecimal equivalent.

In conclusion, Java literals are an integral part of programming. They enable us to declare and use values directly in our code, making our code more readable and maintainable. Understanding their use and nuances helps us communicate effectively with the Java compiler and create robust applications.