When we learnt about Variables and Data Types in Java, I already mentioned that a variable is a placeholder to store any value and use the value during execution. Any application mostly relies on user data input, but in certain cases, we need to declare a variable and store a fixed value in it. The fixed value we use is called a Literal. The question should be, do these literal values vary for different Data Types in Java? And, the answer is yes.
Literal values for different Data Types in Java
For each Data Type in Java, we have literal values. Let’s explore some of those
Integer Literals :
//Decimal Integer Literals
int intLit = 230;
System.out.println("Decimal Integer Literal : " + intLit);
//Octal Integer Literals, starts with 0
int intOctLit = 0230;
System.out.println("Octal Integer Literal : " + intOctLit);
//Hexa Decimal Integer Literals, starts with 0x
int intHexLit = 0x26F;
System.out.println("Hexa Decimal Integer Literal : " + intHexLit);
//Binary Integer Literals, starts with 0x
int intBinLit = 0b101;
System.out.println("Binary Integer Literal : " + intBinLit);
Integer Literals in Java :
------------------------
Decimal Integer Literal : 230
Octal Integer Literal : 152
Hexa Decimal Integer Literal : 623
Binary Integer Literal : 5
Long Literals :
// Long Literals, neds to end with l or L
long longLit1 = 23023423534656478l;
System.out.println("Long Literal with l : " + longLit1);
long longLit2 = 23023423534656478L;
System.out.println("Long Literal with L : " + longLit2);
Long Literals in Java :
------------------------
Long Literal with l : 23023423534656478
Long Literal with L : 23023423534656478
Double and Float Literals :
We already know that, in Java, every number with a decimal point is by default double Data Type. To mention any number as a float, we need to end the number with f or F.
// Double Literal with decimal point
double dblLit1 = 10.56;
System.out.println("Double Literal : " + dblLit1);
// Double Literal with exponential
double dblLit2 = 5e10;
System.out.println("Double Literal with exponential : " + dblLit2);
// Float Literal with decimal point, requires f or F at end
float fltLit1 = 152.34567f;
System.out.println("Float Literal : " + fltLit1);
// Float Literal with exponential, requires f or F at end
float fltLit2 = 100E24F;
System.out.println("Float Literal with exponential : " + fltLit2);
Double and float Literals in Java :
------------------------
Double Literal : 10.56
Double Literal with exponential : 5.0E10
Float Literal : 152.34567
Float Literal with exponential : 1.0E26
char and boolean literals :
// Char Literal with Unicode value
char charLit1 = 'c';
System.out.println("Char Literal Unicode Egglish : " + charLit1);
char charLit2 = 'क';
System.out.println("Char Literal Unicode Hindi : " + charLit2);
// Boolean litearls
boolean areYouLearningJava = true;
System.out.println("Boolean true value : " + areYouLearningJava);
boolean areYouStupid = false;
System.out.println("Boolean false value : " + areYouStupid);
Char and boolean Literals in Java :
------------------------
Char Literal Unicode Egglish : c
Char Literal Unicode Hindi : क
Boolean true value : true
Boolean false value : false
Tried to give you some basic examples on Literals of different Data Types in Java, so that you can have a brief idea to start with.