Any constant value that can be assigned to the variable is called a literal. In Java, a Literal is value of boolean,character,numeric or string data.
int x=103;
//Here 103 is a constant/literal
Types of Literals in Java
Java supports the following types of literals:
- Boolean literals
- Integral literals
- Floating-point literals
- Char literals
- String literals
Boolean Literals in Java
Only two values are allowed for Boolean literals, i.e.,true and false.
boolean b=true;
boolean c=false;
Example
public class BooleanLiteralExample{
public static void main(String args[]){
boolean b=true;
boolean c=false;
System.out.println(“Concept of this program is correct? :”+b);
}
}
Integer Literal
Integer literals in Java are simply whole number literals without any exponential or fractional component.
Integer literals can be specified in four ways, as listed below:
- Decimal Literals(Base 10): In this form,the allowed digits are 0-9.
int decLiteral=134;
2. Octal literals(Base 8): In this form,the allowed digits are 0-7.
//The octal number should be prefix with 0
int octLiteral=0134;
3. Hexadecimal literals(Base 16): In this form,the allowed digits are 0-9, and characters are a-f. One can use both uppercase and lowercase characters.
// The hexa-decimal number should be prefix with 0X or 0x
int x=0X134Abc
4. Binary literals: This form of literals allowed 0 and 1 digits. Literals value should be prefixed with 0b or 0B.
int x=0b1011;
Example:
public class IntegerLiteral{
public static void main(String args[]){
//decimal-form literal
int a=134;
//octal -form literal
int b=0134;
//Hexa-decimal form literal int c=0x134Abc;
//Binary literal
int d=0b10111;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
Character Literal:
Character literals in java are represented by single character enclosed within single quotes(‘ ‘). The character can be any unicode character, including letters, digits, special character and escape sequences.
Here are some examples of character literals in Java:
- Single characters:
char letterZ=’Z’;
char digit8=’8′;
char specialChar=’@’;
2. Ezcape sequences:
char tab=’t’;
char newline=’n’;
char backslash=’\’;
Example
class CharacterLiteralExample{
public static void main(String args[]){
char letterZ=’Z’;
char digit8=’8′;
char specialChar=’@’;
char newline=’n’;
char tab=’t’;
char backslash=’\’;
char singleQuote=’ ‘ ‘ ;
char unicodeChar=’u03A8′;
System.out.println(“Single characters:”);
System.out.println(“Letter Z: “+ letterZ);
System.out.println(“Digit 8:”+digit8);
System.out.println(“Special character:”+ specialChar);
System.out.println(“Escape sequences:”);
System.out.println(“Tab:”+ tab);
System.out.println(“Newline :”+newline);
System.out.println(“Backslash : ” +backslash);
}
}
Floating-point Literal in Java
Floating-point literals can be represented in only decimal form, and one cannot specify in octal and hexadecimal forms.
Decimal literals(Base 10): In this decimal form, the allowed digits are 0-9.
double d=456.123;
Note:
By default,every floating point literal is of double type, and hence one cannot assign directly to the float variable without suffix f or F.
Hexadecimal floating-point literals are not supported in Java.
String Literals in Java:
String literals in java are represented by any sequence of characters within double quotes.
String s=”apple”;