Literal is a value which can be stored in a variable. This value can be used further in the program, as per the requirement.
Literals can be assigned to the following types of variables.
class Jtc{
public static void main(String arg[]) {
int a = 1234567890;
System.out.println(a);
}
}
1234567890
Integral Literals
Floating-Point Literals
Boolean Literals
Character Literals
Null Literals
String Literals
Integral literal is an exact numeric literal. An exact numeric literal is a value without a decimal point.
It can be a –ve or +ve value, such as 234, -56, +45.
For Integral Literals we use byte, short, int and long types of variables.
Integral literals are of four types:
• Decimal literals
• Octal literals
• Hexa-decimal literals
• Binary literals
Rules for applying decimal literals:
o Decimal literals must not start with 0.
o It can be anything between 0 to 9.
public class Jtc {
public static void main(String[] args) {
int a = 1234567890;
System.out.println(a);// 1234567890
// int b = 01234567890; // The literal 01234567890 of type int is out of range
}
}
1234567890
Here, we define a class Jtc. Inside this class we declare int type of variable a and b. Their values are 1234567890 and 01234567890 respectively. We get an error in b because we started the values with 0, that’s why it behaves like Octal type of value, and in Octal system the numbers 8 and 9 are not included.
Octal literals Rules:
• Octal literal must be within 0 to 7 but it must start with 0.
public class Jtc {
public static void main(String[] args) {
int b = 01234567; // it is Octal literal
System.out.println(b);
}
}
342391
Here, we enter an int type of variable which is b and store a value which is 01234567.
Hexa-Decimal Rules:
• A hexa-decimal literal must be within 0 to 9 and a to f or A to F.
• It must start with 0x / 0X.
• Note: a to f and A to F having same meaning.
a|A: 10
b|B: 11
c|C: 12
d|D: 13
e|E: 14
f|F: 15
public class Jtc {
public static void main(String[] args) {
// int a = 123af; // Not-Valid
int b = 0x123af; // valid
int c = 0X123Af; // valid
System.out.println(b);
System.out.println(c);
}
}
74671 74671
Here, we define a Jtc class and in it I declare three int type variables a, b and c. In a variable I am trying to store a value 123af and we have not started with 0x or 0X that is the reason we got an error at compile time.
Binary Literals Rules:
• Binary literals contain 0 and 1.
• It must also start with 0b or 0B.
• It is introduced in JDK 1.7.
public class Jtc {
public static void main(String[] args) {
int a = 10101010; // It is a decimal value.
int b = 01010101; // It is a Octal value.
int c = 0b1010101; // It is a Binary Value.
int d = 0B1010101;// It is a Binary Value.
System.out.println("a :-"+a);
System.out.println("b :-"+b);
System.out.println("c :-"+c);
System.out.println("d :-"+d);
}
}
a :- 10101010 b :- 266305 c :- 85 d :- 85
Here, we define a Jtc class and declare four variables a, b, c and d. In it we store 10101010 which is a decimal value. Here, because we have not started with 0, in b we have 01010101 (1 shifts to extreme right) which is an octal value as it starts with 0. Further we can see in b and c we have values which start with 0b and 0B which are Binary Values.
It is a numerical value with decimal point. It can be –ve or +ve value, such as 12.34, 3.14, -10.67, 56.00 In Java we have two different data types for Floating-Point Literals these are float and double. A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.
The floating-point types (float and double) can also be expressed using E or e (for scientific notation).
Example 1
public class Jtc {
public static void main(String[] args) {
double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1 = 123.4f;
System.out.println("d1 :- "+d1);
System.out.println("d2 :- "+d2);
System.out.println("f1 :- "+f1);
}
}
d1 :- 123.4 d2 :- 123.4 f1 :- 123.4
Example 2
class Floating_Point_Literals {
// float f1=11.22; -> it must be end with f/F.
float f2 = 11.22f;
float f3 = 11.33F;
float f4 = Float.MAX_VALUE;
float f5 = Float.MIN_VALUE;
float f6;
double d1 = 11.555;
double d2 = 526.566D;
double d3 = 11.333d;
double d4 = Double.MAX_VALUE;
double d5 = Double.MIN_VALUE;
double d6 = 221.378D;
double d7 = 99.99e+5;
double d8 = 9.9E-5;
void show() {
System.out.println("show in Hai");
System.out.println(f2);
System.out.println(f3);
System.out.println(f4);
System.out.println(f5);
System.out.println(f6);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
System.out.println(d4);
System.out.println(d5);
System.out.println(d6);
System.out.println(d7);
System.out.println(d8);
}
}
class JTC {
public static void main(String[] args) {
Floating_Point_Literals h1 = new Floating_Point_Literals();
h1.show();
}
}
show in Hai 11.22 11.33 3.4028235E38 1.4E-45 0.0 11.555 526.566 11.333 1.7976931348623157E308 4.9E-324 221.378 9999000.0 9.9E-5
• Boolean literals represent only two values true or false.
• In Java program we are not able to represent true by 1 and false by 0.
• true and false are keywords in Java.
class Boolean_Literals {
boolean b1;
boolean b2 = true;
boolean b3 = false;
// boolean b4=TRUE; -> not ok
// boolean b5=FALSE; -> not ok
// boolean b6=0; -> not ok
// boolean b7=1; -> -> not ok
Boolean b8 = true;
Boolean b9 = false;
Boolean b10;
// Boolean b11=new Boolean(); -> not ok
Boolean b12 = new Boolean(true);
boolean b13 = Boolean.TRUE;
boolean b14 = Boolean.FALSE;
void m1() {
System.out.println("m1 in Hello");
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b8);
System.out.println(b9);
System.out.println(b10);
System.out.println(b12);
System.out.println(b13);
}
}
class JTC {
public static void main(String[] args) {
Boolean_Literals h1 = new Boolean_Literals();
h1.m1();
}
}
m1 in Hello false true false true false null true true
• Anything which is enclosed with pair of ‘ ’ is called Character Literal.
• Using char primitive data types we can store a Character Literal
• Empty Character Literals are not allowed.
• More than one symbol is not allowed in same pair of ‘ ’.
class Character_Literals {
char ch1;
char ch2 = ' ';
char ch3 = ' ';
// char ch4=''; -> Empty Character Literals is not Allowed.
// char ch5=' . '; -> More then one symbol is not allowed in same pair of ' '.
char ch6 = '"';
// char ch7='''; -> not ok
char ch8 = '\u0022';
char ch9 = 'A';
// char ch10='AB'; -> not ok
char ch11 = '*';
// char ch12=' '; -> not ok
// char ch13='123'; -> not ok
char ch14 = '1';
char ch15 = 65;
char ch16 = '\n'; // \n is an escape sequence character which represents new line
char ch17 = '\t'; // \t :- one tab
char ch18 = '\r'; // \r :- Carriage Return
char ch19 = '\\';
// char ch20='\'; -> not ok
// char ch21='//'; -> not ok
char ch22 = '/';
char ch23 = '.';
char ch24 = '%';
char ch25 = '\u0001';
char ch26 = '\'';
int i1 = 'A';
// int i2=A; -> not ok
// int i3='AB'; -> not ok
int i4 = '9';
int i5 = '\u0022';
int i6 = '\\';
// int i7='\'; -> not ok
int i8 = ' ';
// int i9='123'; -> not ok
void m1() {
System.out.println("m1 in Hello");
System.out.println(ch1);
System.out.println(ch2);
System.out.println(ch3);
System.out.println(ch6);
System.out.println(ch8);
System.out.println(ch9);
System.out.println(ch11);
System.out.println(ch14);
System.out.println(ch15);
System.out.println(ch16);
System.out.println(ch17);
System.out.println(ch18);
System.out.println(ch19);
System.out.println(ch22);
System.out.println(ch23);
System.out.println(ch24);
System.out.println(ch25);
System.out.println(ch26);
System.out.println(i1);
System.out.println(i4);
System.out.println(i5);
System.out.println(i6);
System.out.println(i8);
}
}
class JTC {
public static void main(String[] args) {
Character_Literals h1 = new Character_Literals();
h1.m1();
}
}
m1 in Hello " " A * 1 A \ / . % ☺ ' 65 57 34 92 32
Null is the default value of any reference variable but null is not equivalent to 0 .
public class Null_Literals {
String str;
Null_Literals nl;
System s1;
public static void main(String[] args) {
Null_Literals obj = new Null_Literals();
System.out.println(" str : " + obj.str);
System.out.println(" nl : " + obj.nl);
System.out.println(" s1 : " + obj.s1);
}
}
str : null nl : null s1 : null
Here, we define one class as Null_Literals and inside this class we declare variable without initializing a value so when we create an object of class, then JVM will have Null initial.
Anything which is enclosed within double quote’s (“ ”) will be treated as a String literal.
In Java String is not primitive data type but it is a special class which is available in java.lang package.
Whatever, you are enclosing within double quote’s (“ “ ) , it will be printed on your console when you are trying to do so.But in case of escape sequence character it will show it’s original value for what it has been designed.
class String_Literals {
String s1;
String s2 = "abc";
String s3 = "abc123@$";
String s4 = "H1 I am in Jtc";
String s5 = "Hello \t Welcome to Jtc";
String s6 = "Hello \r NA";
// String s7 = """; -> not ok
// String s8 = "\u0022"; -> not ok
String s9 = "\"";
String s10 = \u0022 Hello String literal \u0022;
// String s11 = "\u0022\u0022; -> not ok
String s12 = "C:\\program\\java\\jdk\\bin";
// String s13 = "C:\program\java\jdk\bin"; -> not ok
String s14 = "";
String s15 = 515 + "Hello";
String s16 = "\u0001";
String s17 = " "; // -> tab is ok
String s18 = " "; // -> tab & space ok
// String s19 = ""Hello""; -> not ok
// String s20 = ""String""; -> not ok
String s22 = " ";// -> space is ok
String String;
void m1() {
System.out.println("m1 in Hello");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
System.out.println(s9);
System.out.println(s10);
System.out.println(s12);
System.out.println(s14);
System.out.println(s15);
System.out.println(s16);
System.out.println(s17);
System.out.println(s18);
System.out.println(s22);
System.out.println("String :" + String);
}
}
public class JTC {
public static void main(String[] args) {
String_Literals h1 = new String_Literals();
h1.m1();
}
}
m1 in Hello null abc abc123@$ H1 I am in Jtc Hello Welcome to Jtc NAlo " Hello String literal C:\program\java\jdk\bin 515Hello ☺ String :null