Java Tutorial for Beginners – Literals in Java

java-literals

Literals in Java

Literals are data items with constant or fixed values. As a result, literals are also referred to as constants in Java. Literals can be used to represent a variety of values, including numeric, character, boolean, and String values. In Java, also we can assign literals to any of the eight primitive data types.

For Example:
int num1 = 20;

Here num1 is integer variable and has value 20 which is nothing but Literal.

Literals in Java

Types of Literals in Java

There are 6 different kinds of literals in Java:

  1. Integer literals
  2. Floating Literals
  3. Null literals
  4. Boolean literals
  5. Character Literals
  6. String literals

Java Integer Literals

Whole numbers with no fractional or exponential parts are known as integer literals. They can be either negative or positive, or they may be devoid of any indication. They can be either negative or positive, or they may be devoid of any indication.
When writing integer literals, the rule is that there must be at least one digit and no decimal point. In a literal, there can be positive(+) or negative(-) signs.

Floating Literals

The literals with fractional parts and decimal points are known as floating literals. The real literals are another name for them. We can communicate with them in two ways: fractionally or exponentially. However, We can’t specify them in octal or hexadecimal values; only decimal values are allowed. Between the digits is a decimal point (.).

Boolean Literals

True and false are the only values for Boolean literals. They only contain Boolean values. These are use for assigning flag variables or when a condition needs to be checked. The Boolean literal can only have two values: true or false. So, the boolean data type must be there.

Character Literals

A single character enclosed in a single quote ‘’, as in ‘Z,’ is call to as a character literal. A character literal must contain a single character enclosed within a single quote, according to the rule. Further, to represent a character literal, we must use the char data type.

Escape Sequences in Java

Java String Literals

Multiple characters are represents by the String literals. A string is a collection of zero or more characters enclosed in double quotes ” ”. An escape sequence can be represent by any character in a string literal. As a result, string-Literals are all members of the String class.

For Example:

String s1 = “Abhinav Pandey”;

String s2 =” ” ;

NULL Literals

Null literals, on the other hand, are values that represent null values. There is only one value for null literals (null). It displays a null reference. The null type is symbolize by a null literal.

Conclusion

Finally, Literals in Java are useful for assigning values to variables. Literals are also referred to as constants in Java. The values for the variables can be assign based on their data types. In Java, there are six different types of literals; however, the null literal is rarely use but can still be considered a literal. When writing integer literals, the rule is that there must be at least one digit and no decimal point . Literals can be either negative or positive. Null literals, on the other hand, are values that represent null values.

In this article we learned about literals and in upcoming stay tuned for more interesting blogs…

http://mechomotive.com/java-variables/

If we miss anything in this blog, please do share your thoughts in comment Section. We will be glad to add your Suggestions!!!