Sometimes while creating any program, we need to make some value fixed as for example – display some fixed message “Please input some value“. This can be done using constants in Java. In this post, I will try to clear your concept related to constants available in Java.
Constants refer to the values which can not be changed during the execution of the program. These are the fixed values which are assigned by the programmer at the compile time and we cannot alter its value at runtime or during the execution of the program.
There are basically two types of constants-
- Numeric Constants
- Character Constants
Numeric constants are subcategorized in two parts-
- Integer Constant
- Real Constant
Character constants are subcategorized in two parts-
- Single Character Constants
- String Constants
Numeric Constants refer to the constants or the fixed values which contain digits (0 to 9). In simple words, Numeric constants are the fixed digits assigned to any variable or any element.
For Example-Total Marks = 100;
Here we have assigned the value of total marks is 100 at the compile time and this value can not be changed during the execution of the program.
Also read – Tokens in Java (Keywords, Identifiers, Literals).
Now, the types of numeric constants,
Integer constants refer to the constants or the fixed values made up of digits which can’t alter during execution. It does not contain any decimal value. Integer constants are of 3 types i., decimal integer constants, octal integer constants & the hexadecimal integer constants.
Decimal Integer Constants refer to the constant value which contains digits between 0 to 9 and does not contain any decimal value.
For example-427 289 019 -27 -98
Octal Integer Constants refer to the constants which are made up of octal values i.e., 0 to 7. It can be of any combination.
For Example-087 098 0
Hexadecimal Integer Constants refer to the constants which start with the value 0x and it may contain any sequence of the digit. Apart from digits, a hexadecimal constant may also contain characters between A to F.
A for 10
B for 11
C for 12
D for 13
E for 14
F for 15
For Example-0x9 0xA 0x
Real Constants refer to the constants or the fixed values made up of a sequence of digits (0 to 9) and it also contains the decimal values. Real constants are also termed as floating point constants.
For Example-0.006 0.87 -8.76 4.23
A real constant is of 4 types-
- Whole number constants
- Fractional number constants
- Decimal number constants
Character constants refer to the constant value made up of characters or the group of characters whose value can’t alter during the execution of the program.
For Example-a number b
Character constants are of two types i., single character constants & string constants.
Single Character Constants
Single character constants refer to the constant values consists of a single character i.e., a to z. This single character is enclosed in a single quote mark.
For Example-‘a’ ‘b’ ‘c’ ‘z’ ‘h’
String Character Constants
String character constants refer to the constant value made up of a group of characters. It is enclosed in a double quote mark and it can be of any type like any digit, any special character, alphabets or any blank space. We generally use string character constants to display any message to the user.
For Example-“JAVA” “1879” “K”
Backslash Character Constants
Backslash constants are the special constants declared using a backslash (/). These constants are generally used for the proper formatting of the program.
- Structure of Java Program
- Difference between C++ and Java(C++ vs Java)
- Tokens in Java (Keywords, Identifiers, Literals)
- Introduction to Java Programming