• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Language » Java Literals – What is literal in Java? Type of literal
Next →
← Prev

Java Literals – What is literal in Java? Type of literal

By Dinesh Thakur

Java Literals: A literal in java refers to a fixed value that appears directly in a program. Java define five primary types of literals. By literal we mean any number, text, or other information that represents a value. This means what you type is what you get. They are commonly known as constants. We will use literals in addition to variables in Java statement.

A literal is a constant value that can be classified as integer literals, string literals and boolean literals. An expression is a combination of literals, variables and operators to form an executable statement which computer value.

While writing a source code as a character sequence, we can specify any value as a literal such as an integer. This character sequence will specify the syntax based on the value’s type. This will give a literal as a result. For instance

int month  = 10;

In the above statement the literal is an integer value i.e 10. The literal is 10 because it directly represents the integer value.

Types of Java Literals

In Java programming language there are five type of literals that literals can be classified as integer literals, floating-point literals, character literals, string literals and boolean literals. Java literals can be of primitive data types. The way each literal is represented depends upon it type.

 

Number Literals

Number literals is a sequence of digits and a suffix as L. To represent the type as long integer we use L as a suffix. They are primary literals used in Java programming and come in three different number systems. We can specify the integers either in Decimal (base 10), Octal (base 8), Hexadecimal (base 16). To indicate a decimal format(Base 10) put the left most digit as nonzero. Similarly put the characters as ox to the left of at least one hexadecimal digit to indicate hexadecimal format (Base 16). Also we can indicate the octal format(Base 8) by a zero digit followed by the digits 0 to 7. Lets tweak the table below.

 

Rules for constructing Integer Literals

a) No commas or blank spaces are allowed in an integer literal.
b) It may be either positive or negative. If no sign precedes an integer literal, it is assumed to be positive.
c) It must not have a decimal point.
d) It must have atleast one digit. Eg. 0.
e) The value of Integer literal must lie within the range of integers.

When you write an integer literal, Java automatically assumes that it is oftype into In order to write literal of long type, append a letter ‘I’ or ‘L’ to the end of the number. For example : 480066621212 L. Integer literals are represented in such a way when you need to store very large numbers.
If a literal of type intis directly assigned to a variables of type short and its value is within the valid range for short, the int literal is treated as if it was a short literal. Similarly, for the byte data type.

Character Literals

We can specify a character literal as a single printable character in a pair of single quote characters such as ‘a’, ‘#’, and ‘3’. You must be knowing about the ASCII character set. The ASCII character set includes 128 characters including letters, numerals, punctuations etc. There are few character literals which are not readily printable through a keyboard. For example: You cannot include a newline character by pressing the enter key as this may move the cursor to a new line. Moreover, there are certain other character literals which are interpreted in a different way by the compiler.

For example: You cannot include a single quote (‘) character as it is used to indicate where a character literal begins and ends. For these reasons, Java provides special escape sequences known as backslash character constants. All escape sequences begin with a single backs lash (\) followed by some character that have some specific task to perform.

Unfortunately only a few existing text editors support Unicode characters. So you can use the escape sequence of the form \uXXXX where XXXX are the hexadecimal digits of the Unicode coding for a particular character. This Unicode representation cannot only appear in character and string constant but also in identifiers. For example: To print the copyright symbol, we use the Unicode character \u00A9.

The table below shows the codes that can  represent these special characters. The letter d such as in the octal, hex etc represents a number. 

It is very interesting to know that if we want to specify a single quote, a backslash, or a nonprintable character as a character literal use an escape sequence. An escape sequence uses a special syntax to represents a character. The syntax begins with a single backslash character.

Lets see the table below in which the character literals use Unicode escape sequence to represent printable and nonprintable characters both.

 

Boolean Literals

The values true and false are also treated as literals in Java programming. When we assign a value to a boolean variable, we can only use these two values. Unlike C, we can’t presume that the value of 1 is equivalent to true and 0 is equivalent to false in Java. We have to use the values true and false to represent a Boolean value. Like 
boolean chosen = true;
Remember that the literal true is not represented by the quotation marks around it. The Java compiler will take it as a string of characters, if its in quotation marks.

Booleans are typically used for design making and for controlling the order of execution of a program.
You can declare a variable flag of type boolean as follows,
boolean flag;
NOTE: A boolean is not an integral type as in C++ and integer values cannot be used in place of boolean.

Floating-point literals

Floating-point numbers are like real numbers in mathematics, for example, 4.13179, -0.000001. It contains decimal point or an exponent sign or both which distinguishes it from the integer literals. Java has two kinds of floating-point numbers: float and double. The default type when you write a floating-point literal is double.

Type
Size
Range
Precision
name
bytes
bits
approximate
in decimal digits
float
4
32
+/- 3.4 * 1038
6-7
double
8
64
+/- 1.8 * 10308
15

A floating-point literal can be denoted as a decimal point, a fraction part, an exponent (represented by E or e) and as an integer. We also add a suffix to the floating point literal as D, d, F or f.  The type of a floating-point literal defaults to double-precision floating-point.
The following floating-point literals represent double-precision floating-point and floating-point values.

 6.5E+32 (or 6.5E32) Double-precision floating-point literal
 7D Double-precision floating-point literal
 .01f Floating-point literal

Floating point literals can be expressed in two forms: Standard Notation and Scientific Notation.

Standard Notation: In the standard notation, the floating point numbers have an integer part and a fractional part, with a decimal point in between the two parts. For Example :
130.35,41.,0.56,.67,-.67, -7.71 etc.

Rules for constructing floating point literals : Standard Notation

a) A decimal point should be present.
b) No commas or blanks are allowed within the real constants.
c) It could be positive or negative but default sign is positive. Eg: +762.35,-32.755 etc.
d) It is possible to omit digits before and after the decimal point but not both. Eg : . 76, 86. etc.

Scientific Notation: In the scientific notation, a floating point literal has a mantissa and an exponent. The mantissa part represent the floating point number in standard notation and the exponent part specifies a power of 10 by which the number is to be multiplied. The mantissa part and exponent part should be separated by letter E(uppercase or lowercase). For example: A number 231.54 can be represented in exponential form as 2.3154e2 (i.e. 2.3154×102). Here, 2.3154 represents mantissa and the part after letter e represents exponent part which has a base value 10. Exponential form is useful for representing very large numbers as it avoids writing large number of zero’s in a number. Eg. 6000000 can be written as 6.0e6. The exponent part indicates how many places the decimal point must be moved to change the number to ordinary fractional form.

Rules for constructing floating point literals : Scientific Notation

a) The mantissa part can either be in a decimal form or an integer. It can be preceded by a positive or negative sign. Default sign is positive. Eg. 23.4 can be written as 2.34e1 or 234e-l or 0.234e2.
b) The exponent must have atleast one digit which must be a positive or negative integer. Default sign is positive.
c) Spaces are not allowed in the mantissa part as well as exponent part. Eg. 3 2.46 e- 5, 324. 6e-6 are invalid.
d) Letter e can be written in uppercase or lowercase. Eg 3. 2e5, 3. 2E5.
e) You can leave out decimal point only if you include an exponent. Eg 0.75 can be represented as 75e-2.

When you write a floating point literal, Java automatically assumes that it is of type double. To specify literal of type float, append ‘f or ‘F’ at the end of the number. For example, 53.756f.
A double literal cannot be assigned directly to a float variable even if the value of double is within the valid float range.
NOTE: You can optionally append d or D to specify a double literal.

String Literals

A string literal is a collection of consecutive characters enclosed within a pair of double quotation marks. In Java a string is not a basic data type, rather it is an object. These strings are not stored in arrays as in C language. There are few methods provided in Java to combine strings, modify strings and to know whether to strings have the same value.

We represent string literals as

 

The above text How are you? is a literal string of characters—a series of characters that appear in output precisely as entered — any Java literal string written between double quotation marks. In Java, a literal string cannot be broken and placed on multiple lines.

Lets see some more examples of string literals:
“”    // the empty string
“\””   // a string containing ”
“This is a string”   // a string containing 16 characters
“This is a ” +   // actually a string-valued constant expression,
“two-line string”   // formed from two string literals

Strings can include the character escape codes as well, as shown here:
String example = “Your Name, \”Sumit\””;
System.out.println(“Thanking you,\n Richards \n”);

The following program shows the use of escape sequences.

//Program to show the use of escape sequences.
public class EscSequence {
    public static void main(String[] args) {
        System.out.println(“Btech \nMCA \nBCA”);
        System.out.println(“\”Enclosing within quotes\”“);
    }
}

Null Literals

The final literal that we can use in Java programming is a Null literal. We specify the Null literal in the source code as ‘null’. To reduce the number of references to an object, use null literal. The type of the null literal is always null. We typically assign null literals to object reference variables. For instance
s = null;
An this example an object is referenced by s. We reduce the number of references to an object by assigning null to s. Now, as in this example the object is no longer referenced so it will be available for the garbage collection i.e. the compiler will destroy it and the free memory will be allocated to the other object. Well, we will later learn about garbage collection.

You’ll also like:

  1. MySql Literal Characters in Java Servlet
  2. What is Identifiers,Literals,Operator and Separators
  3. Difference Between Type Conversion and Type Casting
  4. Type Casting in Java
  5. Type Wrapper in java
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW