What Is Literals In Java Full Guide? - FindersAdda : Tutorials of Make Money, Computer or Internet

Monday, 20 November 2017

What Is Literals In Java Full Guide?

A constant value which can be assigned to the variable is called a literal.

eg: - int x=10;

Here, int is a data type or keyword. x is a name of variable or identifier and 10 is a constant value or literal which is assigned with the variable.

There are following types of literals in java.
  1. Integral literals
  2. Floating point literals
  3. Boolean literals
  4. Char literals 
  5. String literals

literals in java

Integral Literals

For integral data types (like byte, short, int, long) we can specify literal value in the following ways.

  • Decimal Literals or Decimal Forms (base 10) and allowed digits are 0 to 9
                             int x = 10;
  • Octal Form (base 8 ) and allowed digits are 0 to 7. Literal value should be prefixed with 0 (zero).
                           int x = 010;

  • Hexadecimal Form (base 16) and allowed digits are 0 to 9 and A to F. For extra digits (a to f), we can use both lower case and upper case characters. This is one of very few areas where java is not case sensitive. The literal value should be prefixed with 0x and 0X.
                       int y = 0x120;

These are only possible way to specify literal value for integral data types.

class First
public static void main(String[] args)
{ int x = 10;
int y = 010;
int z = 0x10;

Output : -

D:\Program\JAVA>javac First.java
D:\Program\JAVA>java First

NOTE : - Programmer having the choice to take the value either decimal, octal or hexadecimal but JVM will always provide the value in decimal form.

  • By default, every integral literal is of int type but we can specify explicitly long type by suffixed with l or L.
int x = 10 ; 
long l = 10L ; 
long l = 10 ;
  • There is no direct way to Specify byte and short literals explicitly but indirectly we can specify. Whenever we are assigning integral literal to the byte variable and if the value within the range of byte then compiler treats it automatically as byte literal. Similarly, short literal also.
byte b = 10; 
byte b =127; 
short s = 32767;
NOTE: - When we assign value more than its(data type) range then we will get compile time error "Possible loss of precision" found: "int" required: "byte".

Floating Point Literal

By default, every floating point literal is of double type and hence we can't assign directly to the float variable. But, we can specify floating point literal as float type by suffixed with f and F.

float f = 12.452f; 
double d = 12.452;

We can specify explicitly floating point literal as double type by suffixed with d or D. off course these convention is not required.

double d = 12.456D; (It is right declaration) 
float f = 12.456D; (It is wrong Initialization

We can Specify floating point literals only in decimal form. And we can't specify in octal and hexadecimal form.

double d = 123.456; (It is right initialization) 
double d = 0123.456; (It is also right initialization
double d = 0x123.456; (It is wrong Initialization)

Point 1: - In the above three initialization of double, you will be thinking that how second initialization is true. Because JVM removes starting zero. That's why, it is possible.

Point 2: - We can assign integral literal directly to floating point variables and that integral literal can be specified either in decimal, octal or hexadecimal form.

double d = 0786; (It is wrong Initialization) 
double d = 0xFace; (It is right  initialization) 
double d = 0786.0; (It is right initialization) 
double d = 0xFace.0; (It is wrong Initialization) 
double d = 10; (It is right initialization) 
double d = 0777;  (It is right initialization)

Explanation: - In the above six double initialization, first initialization is wrong because it is octal type value and 8 is not allowed value in octal. Now, you will be thinking that double data type doesn't support octal value then how this initialization is wrong. This initialization is wrong because its value is not floating point number, it is integral type. Second initialization is right because it is integral type and in hexadecimal form. Third initialization is right because in floating point number, JVM removes first zero.Fourth initialization is wrong because it is floating point number and it is in hexadecimal form which not supported by floating point number. and so on.

Point 3: - We can't assign floating point literals to integral types.

double d = 10; (It is right initialization) 
int x = 10.2; (It is wrong initialization)

Point 4: - We can specify floating point literal even in exponential form (scientific notation)

double d = 1.2e3; (It is right initialization) 
float f= 1.2e3; (It is wrong initialization) 
float f=1.2e3f; (It is right initialization)

The above second initialization is wrong because we can't specify directly float value. We can specify float value by suffixed with f and F.

Boolean Literal

The only allowed values for boolean data type are true or false.

boolean b = true; (It is right initialization)  
boolean b = false;  (It is right initialization)  
boolean b = False;   (It is wrong initialization)  
boolean b = 0;  (It is wrong initialization) 

Explanation: - In the above four declaration, third declaration is wrong because true or false can't write in Capital letter. And fourth declaration is wrong because boolean does not contain other type of value.

class First
public static void main(String[] args)
{ int x = 0;


D:\Program\JAVA>javac First.javaFirst.java:14: error: reached end of file while parsing} ^1 error

Explanation: - The above program will get compiler time error because in java, 0 means not false and 1 means not true. If you want to give false option then you will write false.

Char Literals

We can specify char literal as single characters within single quotes.

char ch = 'a';  (It is right initialization)
char ch = a;  (It is wrong initialization)  
char ch = "a";  (It is wrong initialization)  
char ch = 'ab'; (It is wrong initialization)  

Explanation : - In the above four initialization, the below three initialization is wrong because in char data type , we can specify only withing single quote and single character.

Point 1: - We can Specify char literal as integral literal which represents Unicode value of the character and that integral literal can be specified either in decimal or octal or hexadecimal forms. But allowed range is 0 to 65535.

char ch = 97; (It is right initialization) 
char ch = 0xFace; (It is right initialization) 
char ch = 0777; (It is right initialization) 
char ch = 65536;  (It is wrong initialization)  

Explanation: - In the Above char declaration, last declaration is false because it is out of range.

Point 2: - We can represent char literal in Unicode representation which is nothing but '\uxxxx'.

Here, xxxx is four digit hexadecimal number.

char ch = '\u0061'; (output is a)

Point 3: - Every escape character is a valid char literal.

New Line
Horizontal line
Carriage return
Backspace character
Form feed
Single quote
Double quote
Back slash

String Literal

Any sequence of characters within double quote is treated as String literal.

String s = "Findersadda";

class First
public static void main(String[] args)
{ String str = "Findersadda";

D:\Program\JAVA>javac First.java
D:\Program\JAVA>java First

Final Words

I hope you liked this post so much. If you loved this post, so, share with your friends and also give some suggestion that what should i improve in this post. Thank You So Much......

About Author


Author & Editor

Myself Sumit Kumar Gupta & 21 year's old person. I am a programmer and blogger. I spend much more time on programming and blogging and helps other programmers and bloggers.


Post a Comment

We are eager to see your comments.
Comment Rule :

1. Don't use any link in the comment box. If you have any problem related to link then contact me with the contact form.

2. Don't use the wrong word if you write the wrong word in comment box then it will get spam.