Literals in Java
In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them.
Literal
Literal refers to any constant value that can be used as a variable value.
Literals are an artificial description of Boolean, integer, character, and string data in Java, to put it simply. It is a way for the code to express specific values. The permanent values that are introduced directly into a code are known as literals. It can be automatically assigned to a variable.
There is no need for or usage of calculation because the literals are expressed explicitly in the code. Additionally, because they may be allocated to any primitive form of variable, they are used to optimize the process and make it easier to complete the task.
Types of Literals
Java literals could be divided into six categories.
- Integral Literals
- Floating-point Literals
- Char Literals
- String Literals
- Boolean Literals
Integral Literals
Sequences of integers make up integer literals. Integer literals are divided into four distinct categories. They are:
Decimal Literals
Its base is ten. The permitted digits in this type are 0 to 9.
For example: int q= 123;
Octa Literals
It features an eight-base and accepts digits between 0 and 7. The prefix 0 is required when using an octal literal for Java programming.
For example: int q= 0123;
Hexadecimal Literals
It has a base 16. Hexadecimal supports letters from A to F and numbers from 0 to 9. Hexadecimal literals can be written in either capital or lowercase letters, despite the fact that Java is case sensitive. The hexadecimal number must be represented with 0X or 0x mandatorily.
For example: int q = 0x123;
Binary Literals
Beginning with version 1.7, the only permitted digits for a literal value are 0 and 1. The prefix 0b or 0B must be used before literal values.
For example: int q = 0b1001;
Lets understand along with an example program
File name: IntegralL.java
// A Java application that demonstrates the use of integer literals
public class IntegralL {
public static void main(String[] args)
{
// decimal literal
int q = 123;
// octal literal
int r = 0123;
// Hexadecimal literal
int s = 0x123;
// Binary literal
int t = 0b1001;
System.out.println(q);
System.out.println(r);
System.out.println(s);
System.out.println(t);
}
}
Output
123
83
291
9
Floating-point Literals
For Floating-point data types, we can specify literals in only decimal form, and we cant specify in octal and Hexadecimal forms.
With a leading + or - sign, floating-point literals can denote either a positive or negative number. If nothing else is provided, the value will always be received positively. The use of floating-point literals makes it easier to provide values based on instance needs. For instance, it offers values that may be applied to either float or double cases. It is important to distinguish between integer & floating-point literals since the integer literals have definite integer values while the floating-point literals have either fractional or decimal values instead.
For example: int w= 123.456
File name: FloatL.java
// A java program to demonstrate the use of floating-point literals
public class FloatL {
public static void main(String[] args)
{
// decimal literal
float x = 135.023;
// It also a decimal literal
float y = 0145.289;
System.out.println(x);
System.out.println(y);
}
}
Output
135.023
145.289
Char Literals
Character (Char) literals are arbitrary integer primitive types with the type char. In the Java code, they are character statements with fixed values. The frequency of these sixteen-bit Unicode characters is 0 to 65535. In Java, char literals are written as the character in a single-quote, one beginning quote, and one concluding quote.
For example: char ch = k;
char ch = ‘\U+006B’
File name: CharL.java
// A simple java program to demonstrate the use of char literals
public class CharL {
public static void main(String[] args)
{
// This is a single character in single quotes
char ch= ‘k’;
// Its Unicode representation
char w=’\u006B’;
System.out.println(ch);
System.out.println(w);
}
}
Output
k
k
String Literals
String literals are a series of (zero or more, containing Unicode characters) alphabets or characters enclosed in double quotes. The Java compiler automatically evaluates statements that were defined at compile time, even though string literals may not contain escape line break or newline characters. Only within string as well as character literal, special characters can be escaped using Unicode break sequencing or backlash characters.
For example: String z= “Hello again”;
File name: StringL.java
// Java program that demonstrates the implementation of String literals
public class StringL {
public static void main(String[] args)
{
String f = " Hey, Jimmy";
System.out.println(f);
}
}
Output
Hey, Jimmy
Boolean Literals
Boolean literals are split into two categories because they only support two values.
True: Denotes a real boolean value.
False: Denotes a unreal boolean value.
The logical value is represented by the boolean literals as either true or false. These parameters are not case-sensitive; they are still acceptable whether they are written in uppercase or lowercase.
For example: boolean h= false;
// Java program that demonstrates the implementation of boolean literals
public class BooleanL {
public static void main(String[] args)
{
boolean h = true;
boolean g = false;
System.out.println(h);
System.out.println(g);
}
}
Output
true
false