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

public class IntegerLiterals{
public static void main(String args[]){
int  a= 101;      //decimal form literal
int  b=0100;      //octal-form literal
int  c=0xFace     //Hexa-decimal form literal
int  d=0b1111     //Binary-li
System.out.println("Decimal-form:"+a);
System.out.println("Octal-form:"+b);
System.out.println("Hexa-decimal-form:"+c);
System.out.println("Binary-form:"+d);

Output

Decimal-form:101
Octal-form:64
Hexa-decimal-form:64206
Binary-form:15

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

public class StringLiterals{
public static void main(String[] args)
{
String s = "codedbug";
// If we assign without "" then it treats as a variable and causes compiler error
String s1 = codedbug; 
System.out.println(s);
System.out.println(s1);
}
}

Output

codedbug 
error: cannot find symbol
symbol: variable codedbug 
location: class StringLiterals

String and char types of literals can contain any Unicode characters. For example:

char a = "\u0001";

String a = "\u0001";

Example

public class CharLiterals {
public static void main(String[] args)
{
char ch = 'a';                // signle character literl within signle quote
char b = 0789;                // It is an Integer literal with octal form
char c = '\u0061';            // Unicode representation
System.out.println("character Literal:"+ch);
System.out.println("Integer Literal:"+b);
System.out.println("Unicode Representation:"+c);
}
}

Output

character Literal: a
Integer Literal: error: Integer number too large
Unicode Representation: a

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

Example

public class Test {
public static void main(String[] args)
{
boolean b = true;
boolean c = false;
boolean d = 0;
boolean b = 1;
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}

Output

true
false
error: incompatible types: int cannot be converted to boolean
error: incompatible types: int cannot be converted to boolean