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
Our learners also read: Free java course!
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.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
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 Executive PG Program 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.
What are literals in Java?
A literal is a constant value written in source code. They appear literally in the code and may not change within a particular program run. Literal is the word used to describe the value that appears in source code as opposed to a variable. A literal is a source code representation of a specific data value. For example, the literals in Java include the integer literal 3, the floating-point literal 1.1f, and the character literal 'a'. A Java literal is a representation of a fixed value, similar to a constant, but a literal is a part of a Java language statement and does not need to be declared as a constant. Literals are integral to every Java program because literals are used to represent the values of variables and expressions.
What are the different data types in Java?
Some of the basic data types in Java are: Integer (int), Floating Point (double), Character (char), and Boolean. There are also wrapper classes that are used to support primitive data types. These wrapper classes are Byte, Short, Integer, Long, Float, and Double. The Integer class has a sub-class called Long. The Long class has a subclass called BigInteger. The Character class has six subclasses. Each character is represented in Java by an integer. For example, the character 'a' is represented by 97. The characters 0 to 31 are called the ASCII characters.
Why are strings immutable in Java?
Strings are immutable in Java because immutability is a very good property to have. Immutable objects are easier to reason about than mutable ones. Copying them is simple and fast. Also, it does not waste memory as the memory isn't modified when the strings are passed around. When you pass around mutable objects, the data inside them can be modified by any other object in their path. This is why you should prefer immutable objects over mutable ones whenever you can.