Literals in Java – Integral, Floating-Point, Char, String, Boolean
BY DATAFLAIR TEAM · UPDATED · AUGUST 29, 2019
Literals are number, text, or anything that represent a value. In other words, Literals in Java are the constant values assigned to the variable. It is also called a constant.
For example,
- int x = 100;
So, 100 is literal.
There are 5 types of Literals can be seen in Java. But before we start the discussion, you should revise the concept of Variables in Java.
Types of Literals in Java
1. Integral Literals in Java
We can specify the integer literals in 4 different ways –
- Decimal (Base 10)
Digits from 0-9 are allowed in this form.
- Int x = 101;
- Octal (Base 8)
Digits from 0 – 7 are allowed. It should always have a prefix 0.
- int x = 0146;
Don’t forget to check Datatypes in Java with examples
- Hexa-Decimal (Base 16)
Digits 0-9 are allowed and also characters from a-f are allowed in this form. Furthermore, both uppercase and lowercase characters can be used, Java provides an exception here.
- int x = 0X123Face;
- Binary
A literal in this type should have a prefix 0b and 0B, from 1.7 one can also specify in binary literals, i.e. 0 and 1.
- int x = 0b1111;
Example-
package com.dataflair.literals;
public class IntegralLiteral {
public static void main(String[] args)
{
int decimalValue = 123; // decimal-form literal
int octalValue = 01200; // octal-form literal
int hexaDecimalValue = 0xAce; // Hexa-decimal form literal
int binaryValue = 0b00101; // Binary literal
System.out.println("Decimal form literal is "+decimalValue);
System.out.println("Octal form literal is "+b);
System.out.println("Hexa-decimal form literal is "+hexaDecimalValue);
System.out.println("Binary literal is "+binaryValue);
}
}
Output-
We can specify explicitly as long type by suffixed with l or L but there is no way to specify byte and short, but if it’s in the range the compiler automatically treats it as a byte.
Do you know what is Java Abstract Data Types (ADT)?
2. Floating-Point Literals in Java
Here, datatypes can only be specified in decimal forms and not in octal or hexadecimal form.
- Decimal (Base 10)
package com.dataflair.literals;
public class FloatingPointLiteral
{
public static void main(String args[])
{
double decimalValue = 101.230; // decimal-form literal
double decimalValue1 = 0123.222; // It also acts as decimal literal
double hexaDecimalValue = 1.234e2; // Hexa-decimal form
System.out.println("Decimal form literal is "+decimalValue);
System.out.println("Second Decimal form literal is "+decimalValue1);
System.out.println("Hexa decimal form literal is "+hexaDecimalValue);
}
}
Every floating type is a double type and this the reason why we cannot assign it directly to float variable, to escape this situation we use f or F as suffix, and for double we use d or D.
Have you checked the Latest Career Opportunities of Java?
3. Char Literals in Java
These are the four types of char-
- Single Quote
Java Literal can be specified to a char data type as a single character within a single quote.
- char ch = 'a';
- Char as Integral
A char literal in Java can specify as integral literal which also represents the Unicode value of a character.
Furthermore, an integer can specify in decimal, octal and even hexadecimal type, but the range is 0-65535.
- char ch = 062;
- Unicode Representation
Char literals can specify in Unicode representation ‘\uxxxx’. Here XXXX represents 4 hexadecimal numbers.
- char ch = '\u0061';// Here /u0061 represent a.
- Escape Sequence
Escape sequences can also specify as char literal.
- char ch = '\n';
Example-
package com.dataflair.literals;
public class CharacterLiteral {
public static void main(String[] args)
{
char character = 'd';
//char number = 0789; error: Integer number too large
char unicodeCharacter = '\u0064';
System.out.println(character);
System.out.println(unicodeCharacter);
System.out.println("\" is a symbol");
}
}
4. String Literals
Java String literals are any sequence of characters with a double quote.
- String s = "Hello";
They may not contain unescaped newline or linefeed characters.
However, the Java compiler will evaluate compile-time expressions.
Example-
- package com.dataflair.literals;
- public class StringLiteral {
- public static void main(String[] args)
- {
- String myString = "Hello! Welcome to DataFlair";
- // If we assign without "" then it treats as a variable
- // and causes compiler error
- // String myString1 = Hello;
- System.out.println(myString );
- }
- }
5. Boolean Literals
They allow only two values i.e. true and false.
- boolean b = true;
Example –
- package com.dataflair.literals;
- public class BooleanLiteral {
- public static void main(String[] args)
- {
- boolean boolVar1 = true;
- boolean boolVar2 = false;
- // boolean boolVar3 = 0; error: incompatible types: int cannot be converted to boolean
- // boolean boolVar1 = 1; error: incompatible types: int cannot be converted to boolean
- System.out.println(boolVar1);
- System.out.println(boolVar2);
- }
- }
Summary
Literals in Java are an important concept, which will help you to build your basics in programming. Now, you can implement 5 types of literals in your program.
Just practice and implement!!
Nourish your fundamentals with Operators in Java