Literals in Java

Literals in Java are a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables or we can say Literal is the source code representation of a fixed value. Literals are represented directly in your code without requiring computation.

Java language specifies five major types of literals. Literals in Java can be any number, text, or other information that represents a value. We will use literals in addition to variables in Java statement. They can be assigned to any primitive type variable.

Example

byte a = 68
char a = 'C'
String str = "Welcome to codedbug"
byte, int, long and short can be expressed in decimal (base 10), hexadecimal (base 16) or octal (base 8) number systems as well.
Prefix 0 is used to indicate octal and prefix 0X indicates hexadecimal when using these number systems for literals. For example,
int decimal = 100;
int octal = 0144;
int float = 1.053f;
int hexa = 0x64;

Example

Output

String literals in Java are specified as they are in most other languages by enclosing a sequence of characters between a pair of double quotes. Example of String literals are:
"Hello codedbug"
"two\nlines"
"\" This is in quotes\" "

Example

Output

String and char types of literals can contain any Unicode characters. For example:
char a = "\u0001";
String a = "\u0001";

Example

Output

Only two values are allowed for boolean literals: true and false

Example

Output

Leave a Reply

Contact Us
close slider
Contact Us

How can we help you with?