Values such as 100, 45.5, 'A', "Java", true, and null are examples of literals. Java provides different literal types so that different forms of data can be represented properly. Understanding literals is important because they are widely used in variables, operators, conditions, loops, methods, and object handling.
Table of Contents
Types of Literals in Java
- Integer Literals: Integer literals are used to store whole numbers without decimal values.
- Floating Point Literals: Floating point literals are used to store decimal numbers.
- Character Literals: Character literals are used to store a single character value.
- String Literals: String literals are used to store text enclosed inside double quotes.
- Boolean Literals: Boolean literals are used to store logical values such as true and false.
- Null Literal: Null literal represents the absence of an object reference.
Integer Literals in Java
Integer literals represent whole numbers and do not contain decimal values. Java supports multiple formats for writing integer literals.Types of integer literals:
1. Decimal Literals: Decimal literals use digits from 0 to 9.
Syntax:
Example:datatype variable = number;
2. Binary Literal: Binary literals use only 0 and 1. They start with 0b or 0B.int num = 50;
Syntax:
Example:datatype variable = 0bvalue;
3. Octal Literal:int binary = 0b1010;
Octal literals use digits from 0 to 7 and begin with 0.
Syntax:
Example:datatype variable = 0value;
4. Hexadecimal Literal:int octal = 021;
Hexadecimal literals use digits 0–9 and letters A-F. They begin with 0x or 0X.
Syntax:
Example:datatype variable = 0xvalue;
Below is the Java program to implement all integer literals:int hex = 0x2A;
// Java program to implement
// Integer Literals
public class IntegerLiteralExample
{
public static void main(String[] args)
{
int decimal = 50;
int binary = 0b1010;
int octal = 021;
int hexadecimal = 0x2A;
System.out.println("Decimal Value = " + decimal);
System.out.println("Binary Value = " + binary);
System.out.println("Octal Value = " + octal);
System.out.println("Hexadecimal Value = " + hexadecimal);
}
}
Output:
Explanation:Decimal Value = 50
Binary Value = 10
Octal Value = 17
Hexadecimal Value = 42
- 50 represents a decimal literal.
- 0b1010 represents a binary literal.
- 021 represents an octal literal.
- 0x2A represents a hexadecimal literal.
Floating Point Literals in Java
Floating point literals store values containing decimal points and are generally used with float and double.Syntax:
Below is the Java program to implement floating-point literals:float variable = valueF;
double variable = value;
// Java program to implement
// Floating-Point Literals
public class FloatingLiteralExample
{
public static void main(String[] args)
{
float marks = 87.5F;
double salary = 65000.75;
System.out.println(marks);
System.out.println(salary);
}
}
Output:
Explanation:87.5
65000.75
- 87.5F is a float literal.
- 65000.75 is a double literal.
Character Literals in Java
Character literals store a single character enclosed inside single quotes.Syntax:
Below is the Java program to implement character literals:char variable = 'value';
// Java program to implement
// Character Literals
public class CharacterLiteralExample
{
public static void main(String[] args)
{
char letter = 'A';
char symbol = '#';
char unicode = '\u0043';
System.out.println(letter);
System.out.println(symbol);
System.out.println(unicode);
}
}
Output:
Explanation:A
#
C
- 'A' stores an alphabet.
- '#' stores a symbol.
- '\u0043' stores Unicode value C.
String Literals in Java
String literals store text values enclosed inside double quotes.Syntax:
Below is the Java program to implement string literals:String variable = "text";
// Java program to implement
// String Literals
public class StringLiteralExample
{
public static void main(String[] args)
{
String language = "Java";
String city = "Delhi";
System.out.println(language);
System.out.println(city);
}
}
Output:
Explanation:Java
Delhi
- "Java" and "Delhi" are string literals.
- Strings are always written inside double quotes.
Boolean Literals in Java
Boolean literals store logical values and are mostly used in conditions and decision-making.Java supports:
- true
- false
Below is the Java program to implement boolean literals:boolean variable = true;
boolean variable = false;
// Java program to implement
// Boolean Literals
public class BooleanLiteralExample
{
public static void main(String[] args)
{
boolean passed = true;
boolean rain = false;
System.out.println(passed);
System.out.println(rain);
}
}
Output:
Explanation:true
false
- true means the condition is correct.
- false means the condition is incorrect.
Null Literal in Java
Null literal indicates that a reference variable does not point to any object.Syntax:
Below is the Java program to implement null literal:datatype variable = null;
// Java program to implement
// Null Literal
public class NullLiteralExample
{
public static void main(String[] args)
{
String name = null;
System.out.println(name);
}
}
Output:
Explanation:null
- null means no object reference exists.
- It cannot be assigned to primitive data types.
Common Mistakes While Using Literals in Java
1. Using Double Quotes for Character Literals: Character literals must always be enclosed inside single quotes because double quotes are used for strings. Many beginners use double quotes for storing a single character.Incorrect:
Correct:char grade = "A";
2. Forgetting F in Float Literals: Students often assign decimal values to float variables without using F. Java treats decimal values as double by default, so adding F is necessary for float literals.char grade = 'A';
Incorrect:
Correct:float price = 45.5;
3. Writing Invalid Octal Numbers: Octal literals can contain only digits from 0 to 7. Using digits like 8 or 9 inside octal literals causes compilation errors.float price = 45.5F;
Incorrect:
Correct:int num = 089;
4. Assigning Null to Primitive Data Types: Some beginners try assigning null to primitive variables. The null literal works only with reference types and cannot be used with primitive data types.int num = 067;
Incorrect:
Correct:int number = null;
5. Using Single Quotes for String Literals: Single quotes are sometimes mistakenly used for storing text. Strings must always be enclosed inside double quotes because single quotes are reserved for character literals only.Integer number = null;
Incorrect:
Correct:String language = 'Java';
String language = "Java";
Conclusion
Literals in Java are constant values directly written inside programs. Java provides integer, floating point, character, string, boolean, and null literals to represent different types of data. Integer literals further include decimal, binary, octal, and hexadecimal forms. Understanding these literals helps beginners write programs correctly and understand Java syntax more easily.
Frequently Asked Questions
1. What are literals in Java?
2. How many types of literals are available in Java?Literals are fixed values written directly in Java programs such as numbers, text, characters, and logical values.
3. What are the types of integer literals in Java?Java supports six types of literals: integer, floating point, character, string, boolean, and null literals.
4. Can null be assigned to primitive data types?Integer literals are divided into decimal, binary, octal, and hexadecimal literals.
5. Why is F used with float values?No, null cannot be assigned to primitive types like int, char, and boolean.
F tells Java that the value belongs to the float data type.
Example:
float value = 45.5F;
0 Comments