Programming needs to be optimized for efficiency, faster outputs, and memory. Variables are key in programming that store data at a particular memory location. While executing a Java program, it stores values in containers called variables, a basic storage unit. To enhance the program’s readability, one needs to follow particular conventions while naming variables and assigning values. A source code representing a fixed value is called ‘literal’.
Literals in Java are defined directly in the code without any kind of computation. Any primitive type variables are assigned using literals. Java has a generic, class-based, reflective, imperative, multi-paradigm, and is an object-oriented programming language.
One of the popular programming languages has different data types, viz. primitive data types, and non-primitive data types. Primitive data types include int, byte, short, float, boolean, double, and char, whereas non-primitive data types include arrays, string, and classes.
This article is focused on the ‘Literals in Java’. It covers the concept and types of literals used in Java and their application in programming. After reading this article, readers will have a clear understanding of the literals, how and where to use specific literals while coding in Java.
Literals in Java
Literal in Java is a synthetic representation of boolean, numeric, character, or string data. It is a means of expressing particular values in the program, such as an integer variable named ‘’/count is assigned an integer value in the following statement.
int count = 0;
A literal ‘0’ represents the value zero.
Thus, a constant value assigned to the variable can be referred to as literal.
Literals in Java can be classified into six types, as below:
- Integral Literals
- Floating-point Literals
- Char Literals
- String Literals
- Boolean Literals
- Null Literals
These literals are again specified in different sub-types, let us see one by one in the article.
1. Integral Literals
Integral literals are specified in four different ways, as follows:
Decimal: It has base ten, and digits from 0 to 9.
Int x = 108;
Octal: It has base eight and allows digits from 0 to 7. While assigning an octal literal in the Java code, a number must have a prefix 0.
int x = 0745;
It has base 16. Hexadecimal allows digits from 0 to 9, and characters from A to F. Even though Java is case sensitive, and it also provides an exception for using either uppercase or lowercase characters in the code for hexadecimal literals.
int x = 0X123Fadd;
It can be specified in binary literals, that is 0 and 1 with a prefix 0b or 0B.
int x = 0b1011;
2. Floating-Point Literals
FLoating-point literals can be expressed using only decimal fractions or as exponential notation.
decimalNumber = 89d;
decimalNumber = 3.14159e0;
decimalNumber = 1.0e-6D;
Floating-point literals can indicate a positive or negative value, leading + or – sign respectively. If not specified, the value is always considered positive. It can be represented in the following formats:
-Integer digits (representing digits 0 through 9) followed by either a suffix or an exponent to distinguish it from an integral literal.
-integer digit. integer digit
– integer digit
An optional exponent of the form might be as below:
-an optional exponent sign + or –
-the exponent indicator e or E
–integer digit representing the integer exponent value
An optional floating-point suffix might be as below:
Single precision (4 bytes) floating-point number indicating either for F
Double precision (8 bytes) floating-point number indicating d or D
3. Char Literals
Character (Char) literals have the type char and are an unsigned integer primitive type. They are constant value character expressions in the Java program. These are sixteen-bit Unicode characters that range from 0 to 65535. Char literals are expressed as a single quote, a single closing quote, and the character in Java.
Char literals are specified in four different ways, as given below:
Single quote: Java literal is specified to a char data type as a single character enclosed in a single quote.
char ch = ‘a’;
Char Literal: Java literal is specified as an integer literal representing the Unicode value of a char. This integer can be specified in octal, decimal, and hexadecimal, ranging from 0 to 65535.
char ch = 062;
Escape Sequence: Every escape char can be specified as char literal.
char ch = ‘\n’;
Unicode Representation: Java literal is specified in Unicode representation ‘\uzzz’, where zzzz are four hexadecimal numbers.
char ch = ‘\u0061’;
4. String Literals
A sequence of (zero or more including Unicode characters) characters within double quotes is referred to as string literals.
String s = “Hello”;
String literals may not have unescaped line feed or newline characters, but the Java compiler always evaluates compile-time expressions. Unicode escape sequences or special characters can be used within the string and character literal as backlash characters to escape special characters, as shown in the table below:
5. Boolean Literals
Boolean literals allow only two values and thus are divided into two literals:
True: it represents a real boolean value
False: it represents a false boolean value
boolean b = true;
boolean d = false;
6. Null Literals
Null literal is a particular literal in Java representing a null value. This value refers to no object. Java throws NullPointerException. Null often describe the uninitialized state in the program. It is an error to attempt to dereference the null value.
Literals in Java help build basics in programming. Every Java programmer must be aware of this fundamental and essential concept that assigns values to the program’s variables. As null literal is not much used, commonly only the first five literal types are applied. It is necessary to follow the rules and maintain the correct syntax while using any literal in Java.
upGrad provides support to build your skills in Java that also includes Literals. If you are looking for a platform to develop your skill-set in Java programming, then upGrad has the best learning platform, hands-on practice assignments, and guidance.
If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.