Constant in Java: Numeric and Character Constants

Constant means fixed value which is not change at the time of execution of program. In Java, there are two types of constant as follows

A. Numeric Constants

There are two types of Numeric Constants

1. Integer Constant

An Integer constant refers to a series of digits. There are three types of integer as follows.

Decimal integer Embedded spaces, commas and characters are not allowed in between digits.
For example: 23, 411, 700000, 17.33 etc.
Octal integer It allows us any sequence of numbers or digits from 0 to 7 with leading 0(Zero) and it is called as Octal integer.
For example: 00, 011, 0425 etc.
Hexadecimal integer It allows the sequence which is preceded by 0X or 0x and it also allows alphabets from 'A' to 'F' or 'a' to 'f' ('A' to 'F' stands for the numbers '10' to '15') it is called as Hexadecimal integer.
For example: 0x7, 00X, 0A2B etc.

2. Real Constant

It allows us fractional data and it is also called as floating point constant.

It is used for percentage, height and so on. For example: 0.0234, 0.777, -1.23 etc.

B. Character Constants

There are four types of Character Constants

1. Character Constant

It allows us single character within pair of single coute.

For example: 'A', '7', '/' etc.

2. String Constant

It allows us the series of characters within pair of double coute.

For example: "welcome","END OF PROGRAM", "bye...bye", "A" etc.

3. Symbolic Constant

In Java program, there are many things which is requires repeatedly and if we want to make changes then we have to make these changes in whole program where this variable is used. For this purpose, Java provides ‘final’ keyword to declare the value of variable as follows.

Syntax : final type Symbolic_name=value;

final float PI=3.1459;

The condition is, Symbolic_name will be in capital letter( it shows the difference between normal variable and symbolic name) and do not declare in method.

4. Backslash character Constant

Java support some special character constant which are given in following table.

Constant

mportance

'\b'

Back space

'\t'

Tab

'\n'

New line

'\\'

Backslash

'\"

Single coute

'\"'

Double coute

Previous Post Next Post

Contact Form