Categories: Java

Literals in Java

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

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago