Literals in Java

  • April 27, 2022
  • Java
method overloading and method overriding

Literals(often referred to as constants) are data items that are fixed data values. Java allows several kinds of literal:

  • integer-literal
  • floating-literal
  • boolean-literal
  • character-literal
  • string-literal
  • null-literal
Integer Literal:

Integer Literals are whole numbers without any fractional part. The method of writing integer constants has been specified in the following rule:

An integer constant must have at least one digit and must not contain any decimal point. It may contain either a + or – sign. A number with no sign is assumed to be positive. Commas cannot appear in an integer constant.

Java allows three types of integer literals:

  • Decimal(base 10)
  • Octal(base 8)
  • Hexadecimal(base 16)
Floating Literal:

Floating literals are also called real literals.

Real literals are numbers having fractional parts. These may be written in one of the two forms called fractional form to the exponent form.

The rule for writing a real constant in fractional form is given below:

A real literal in the fractional form must have at least one digit before a decimal point and at least one digit after the decimal point. It may also have either a + or – sign preceding it. A real literal with no sign is assumed to be positive. For instance, 2.0, 17.5, -13.0, etc.

Boolean Literal:

The boolean type has two values, represented by the literals true and false, formed from ASCII letters.

A boolean literal is always of two types, boolean. It is either boolean value true or boolean value false.

Character Literals:

A character literal is one character enclosed in single quotes, as in ‘z’. The rule for writing character constant is given below:

A character literal in Java must contain one character and must be enclosed in single quotation marks.

Note: also read about the Token and keywords

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *