Programming must be optimised for efficiency, output speed, and memory usage. Variables are important in programming because they store data in a specific memory address. A Java programme keeps values in variables, which are basic storage units, while it is running. When naming variables and assigning values, certain rules must be followed to improve the readability of the programme. The term 'literal' refers to source code that represents a definite value. In Java, literals are defined directly in the code and do not require any computation.
Literals are used to assign any primitive type variables. Java is an object-oriented programming language with a generic, class-based, reflective, imperative, multi-paradigm syntax. Different data types, such as primitive and non-primitive data types, are available in one of the most popular programming languages. Non-primitive data types include arrays, strings, and classes, whereas primitive data types include int, byte, short, float, boolean, double, and char. The focus of this article is on 'Literals in Java.' It introduces the notion of literals in Java, as well as the many sorts of literals and how they are utilised in programming. After reading this article, readers will have a better knowledge of literal, as well as how and when to utilise them in Java code.
Literal is a Java construct that represents boolean, integer, character, or string data as a synthetic representation. It is a way of expressing specific values in a programme, for as assigning an integer value to an integer variable labelled "/count in the following sentence.
int count = 0;
A literal ‘0’ represents the value zero.
As a result, a literal value set to a variable can be referred to.
Literals in Java are divided into six categories, as shown below:
Integral Literals
Floating-point Literals
Char Literals
String Literals
Boolean Literals
Null Literals
These literals are again specified in separate sub-types, which we will go over in the essay one by one.
1. Integral Literals
Integral literals are specified in four different ways, as follows:
Decimal: It has base ten, and digits from 0 to 9.
For example:
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.
For example:
int x = 0745;
Hexadecimal:
It has a base of 16 points. Hexadecimal accepts numerals 0 through 9 and letters A through F. Despite the fact that Java is case sensitive, it allows for the use of either capital or lowercase letters in hexadecimal literals.
For example:
int x = 0X123Fadd;
Binary:
It can be specified in binary literals, that is 0 and 1 with a prefix 0b or 0B.
For example:
int x = 0b1011;
2. Floating-Point Literals
FLoating-point literals can be expressed using only decimal fractions or as exponential notation.
For example:
decimalNumber = 89d;
decimalNumber = 3.14159e0;
decimalNumber = 1.0e-6D;
The leading + or – sign in floating-point literals can represent a positive or negative number. If no value is supplied, the value is always assumed to be positive. It's available in the following formats:
To distinguish it from an integral literal, it has integer digits (representing digits 0 through 9) followed by either a suffix or an exponent.
- Integer digit.
- 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 are an unsigned integer primitive type with the type char. In a Java application, they are constant value character expressions. These are Unicode characters with a 16-bit value ranging from 0 to 65535. In Java, a char literal is written as a single quotation, a single closing quote, and the character.
As shown below, char literals can be defined in four different ways:
Single quote: A single character contained in a single quote is specified as a Java literal for a char data type.
For example:
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.
For example:
char ch = 062;
Escape Sequence: Every escape char can be specified as char literal.
For example:
char ch = ‘\n’;
Unicode Representation: Java literal is specified in Unicode representation ‘\uzzz’, where zzzz are four hexadecimal numbers.
For example:
char ch = ‘\u0061’;
4. String Literals
String literals are a sequence of (zero or more, including Unicode characters) characters enclosed in double quotations.
For example:
String s = “Hello”;
Although string literals may not contain unescaped line feed or newline characters, compile-time expressions are always evaluated by the Java compiler. To escape special characters, Unicode escape sequences or special characters can be used as backlash characters within the string and character literal.
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
For example:
boolean b = true;
boolean d = false;
6. Null Literals
In Java, a null literal is a special literal that represents a null value. There is no object associated with this value. NullPointerException is thrown by Java. The uninitialized state of a programme is frequently referred to as null. Attempting to dereference the null value is a mistake.
Literals in Java aid in the development of programming fundamentals. This fundamental and crucial idea, which sets values to the program's variables, must be understood by every Java programmer. Because the null literal is rarely utilised, only the first five literal types are usually employed. When utilising any literal in Java, it is vital to follow the rules and maintain proper syntax.