What is literals in java Programming? Explain with Example

Literals in Java programming is a constant value which is compatible to a data type, programmers assign to a variable that compiler understand.

It is used to assign a value to variables, to compare values or define constants.

Literals are often used to initialize variables. Literals represent numerical (integer or floating-point), character, boolean or string values.

For example :

Types of literals : integer literal, string literal and char literal etc.

int  i  = 1000; // here 1000 is called a literal/
float a = 1000.50; // 1000.50 is float literal
String  s = "Interview Sansar"; //string literal
String s =”A” //string literals
char a =’A’; // char literals

//NOTE : Notice the difference between “A” and ‘A’ . 
//They are string and char literals respectively.

Boolean Literals : There are two types of Boolean literal i.e “true” and “false”

NOTES:

Almost every java developers uses literals every day. But, when asked this question in an interview, many developers started thinking of answer. Reason is simple that we don’t focus on word “literals”  during programming, but just initialization of string, int , float and Boolean etc.

Related Posts