Literal in Java? How many types of literals are allowed in Java?

Q2. What are literal in Java? How many types of literals are allowed in Java?

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

  1. Integer-literal
  2. Floating-literal
  3. Boolean literal
  4. Character-literal
  5. String-literal
  6. the null literal

1.Integer-literal: Integer literal 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 + 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:

  1. Decimal (base 10)
  2. Octal (base 8)
  3. hexadecimal (base 10)

 

  1. Decimal (base 10) Integer Literals: An integer literal consisting of a sequence of digits is taken to be decimal integer constant unless it begins with 0 (digit zero). For instance, 1234, 42, +98, -87 are decimal literals.
  2. Octal (base 8) Integer Literals: A sequence of digits starting with 0 (digit zero) is taken to be an octal integer. For instance decimal integer 8 will be written as 010 as octal integer.(810=108). But make sure that when an integer begins with 0, it must not contain 8 and 9 as these are invalid octal digits.
  3. Hexadecimal (base 10) Integer Literals: A sequence of digits preceded by 0x or 0X is taken to be an hexadecimal integer. FOr instance decimal 12 will be written as 0XC as hexadecimal integer. But with Hexadecimal constants only 0-9 and A-F can be used. All other letters are illegal.

2.Floating-literal: Floating literals are also called real literals.

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

A real literal in fraction form consist of sighed or unsigned digits including a decimal point between digits. The rule for writing a real constant in fractional form is given below:

A real literal in 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 + or – sigh preceding it. A real literal with no sign is assumed to be positive.

The following are valid real literal in fractional form:

2.0, 15.5, -13.0, -0.00435

A real literal in exponent form consist of two parts: mantissa and exponent.

For instance 5.8 can be written as 0.58 x 10=0.58E01 where mantissa part is 0.58( the part appearing before E) and exponent part is 1 ( the part appearing after E). The rule for writing a real literal in exponent form is given below:

A real literal in exponent form has two parts: a mantissa and an exponent. The mantissa must be either an integer or a proper real literal. The mantissa is followed by a letter E or e and the exponent. The exponent must be an integer. 

The following are valid real literal in exponent form:

152E05, 1.52E07, 0.152E08

3.Boolean Literal: The boolean type has two values.A Boolean literal is always of type boolean. It is either boolean value true or boolean value false.

4.Character-Literal: A character literal in Java must contain one character and must be enclosed in single quotation marks.

The following are examples of char literals:

‘a’  ( single character in single quotes)

‘%’  ( single character in single quotes)

‘\\’  ( escape sequence in single quotes)

5.String-Literal: ‘Multiple Character’ constants are treated as string-literals. The rule of writing string-literal is given below:

A string literal is a sequence of zero or more characters surrounded by double quotes. Each character may be represented by an escape sequence.

A string-literal is of class type String. Following are legal string literals:

“abc”, “/ab”, “Seema\’s pen”

6. The Null Literal: The null type has one value, the null reference, represented by literal null, which is formed from ASCII characters. A  null literal is always of null type.

 

Leave a Reply

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

38 Views
%d bloggers like this: