What is constant in java and it’s types?

In this article, we will learn about what is constants and types of constant in java

Constant in Java refers to a fixed value that doesn’t change during the execution of a program. The value of constants appears right in a program. It is also known as Literals. We use the constants to create values that assign to variables. Constants can make our program easy to read and understood by others.

Point to remember

  • The name of the identifier should be in capital letters that we want to declare as constant. For example:


  • As we use the private access-specifier before the constants name. At that point, we can’t change the value of the constant of that particular class.
  • And if we use the public access-specifier before the constants name. In the program, the value of the constant can be change.

Types of Constant in java

There are several types of constant in java such as:

  • Integer Constants
  • Real Constants
  • Character Constants
  • String Constants

Integer Constants

An integer constant refers to the sequence of digits. Without any decimal point, numbers are integers. They are the primary constant in java programming. There are three types of integers as Decimal (base 10), Octal(base 8), Hexadecimal(base 16).

Decimal Integer Octal Hexadecimal
It contain any combination of digits from the set of 0 through 9. It include any combination of digits ranging from the set of 0 to 7 It contain any combination of digits from the set of 0 through 9 or one of the letters a to f or A to F representing decimal values 10 to 15
If the constant consists of two or more digits, the first digit must be something other than 0 because java compiler thinks it an Octal constantThe first digit should be 0, in order to identify the constant as an OctalA hexadecimal integer constant must begin eigher with 0x or 0X.
For Example:
0, 8 , 16, 6589
For Example:
00, 010, 020, 02230.
For Example:
0x0, ox04, oX01000.

Rules for constructing integer constant.

1.No commas or blank spaces should not allow in an integer Constant.

For example:

Valid Invalid
20 ; 034 ; 0xa14,99 ;0x 23 ;11 00

2. It uses both positive or negative signs. If no sign precedes an integer constant, it is positive by default.

For example:

20 ; -34 ; 4499;4-99 ; 0x -23 ; 11- 20

3. It must have decimal

4. it should have at least on digit.

Real Constants

They are also known as the floating-point constant. It must include a decimal point. They are also written in the floating-notation. In which the constant is impart into a mantissa and exponent. Such as 64.23f, -23.43f, 342E-3F , etc. They also can be positive or negative.

Rules for constructing Real constant.

1.It should have a decimal.

For example:

23.7 ; 2.8 ; 1298.0122 ; 290 ; 1889

2..No commas or blank spaces not accepted in a Real Constant.

For example:

20.7 ; 7.6 ; 1298.012.2 , 29, 0 ; 18 89

3. It should have at least one digit, Which can be a positive or negative integer. The default sign is positive.

For example:

23.7E+1 ; 234e-122.0e ; -290e ; +889e

Character Constants

A Character Constants refer to a single Unicode character and enclosed with a pair of single quotation marks (‘ ‘).

For example: ‘d’ , ‘R’ , ‘S’ ,’6′ , ‘$’ etc.

Character constants can represent either the printable character or the non-printable character. Examples of printable character constants are ‘a’, ‘5’,’?’, etc. . But there are a few character constants that are not included in a program directly, Through the keyboard as backspace, newline, and so on. These character constants are also known as non-printable constants. 

They can include in the program by using an escape sequence. 

An escape sequence refers to a character preceded by the backspace(\).

Some of the escape sequences used in java are listed below.

escape sequence character constants
\fForm feed
\rCarriage return
\”Double quote
\’Single quote

String Constants

It refers to a sequence of any number of characters enclosed in the double quote( ” “). The character can be alphabets, digits, special characters, and blank space.

For example:

“Apple” , “20981” , ” ” , “@#$”, etc.

Recommended posts

What is JavaScript?

Difference between the Java and JavaScript

Implementation of Array class in JavaScript

jQuery Introduction



Leave a Reply

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