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.




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.