Java Constants
Constants in Java refer to fixed values that do not change during the execution of the program. A constant is a variable whose value cannot change once it has been assigned.
To define a variable as a constant, we just need to add the keyword “final” in front of the variable declaration.
Syntax:
Example:
The above statement declares the float variable “pi” as a constant with a value of 3.14f. We cannot change the value of "pi" at any point in time in the program.
Types of Constants
i) Integer Constants : It refers to sequence of digits . There are three types of integers namely decimal integer , octal integer and hexadecimal integer.
ii) Real Constants: Integer numbers are inadequate to represent quantities that are very continuously, such as temperatures, prices, heights ,etc. These numbers are represented by fractional parts. Such numbers are called real constants.
iii) Single Character Constants : It contains a single character enclosed within a pair of single quote marks.
iv) String Constants: It contains a single character enclosed within a pair of double quote marks. Example: “hello", “2000”, “7+5”.