Types of Constants in Java

Whenever we declare a constant in java its value tends to be the same throughout the entire program. In the case of a normal variable, we can change the assigned value. Java doesn’t allow the use of constants directly so we use static and final non-access modifiers.

The main motive of using constants is to decrease repeatability in the code with some standard values. It is cached by the Java Virtual Machine and written code results in improvement of the performance.

Let’s see the below example for declaring constant with the final keyword:

In the above code, we have declared a variable mod having 1000000007 as a value inside it. We have written “final” at the starting of the code stating that this cannot be changed at any point inside the code. You can take the value of constant as an input from the user too as it is not compulsory to assign values during declaration.

If we try to change the value of a final variable then the compiler will show an error in compile time and run time as well. Let’s see the below example.

Output:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The final local variable mod cannot be assigned. It must be blank and not using a compound assignment
at constants.main(constants.java:6)

Declaring Constant Outside Main Method

In the above code, static keyword is used while declaring the standard_temp variable so that it can be called inside the main method.

So these were some of the ways to declare constants. I hope you have understood all of them. Now let’s see the types of constants.

Types of Constants in Java

Mainly there are two types of constants in java i.e, Numeric constant and Character constant but they have further types which we will be seeing below:

1. Integer Constants

These constants contain an order of digits that can either be positive or negative in nature. They are further divided into decimal integer constants, octal integer constants, and Hexadecimal constants.

Decimal Integer

It is an order of integers starting from 0 till 9, and they can be either negative or positive. It’s up to you to specify the plus sign or not as it is optional.

Example: The 55, -44, +546 numbers are valid whereas numbers like 11.01, 45 56, @12 are not valid because they either include some special characters, space, or decimal.

Octal Integer

Octal means 8 therefore it is an order of integers starting from 0 till 7, but it always starts from 0. So you need to remember that 8 & 9 cannot be written in the octal representation of any number.

Example: The 033, 0,0111 numbers are valid.

Hexadecimal Integer

These constants can contain digits from 0 to 9, and also alphabets starting from a to f both in small as well as in the capital. They always start either from 0x or 0X.

Example: The 0x22, 0xA2, 0X2B numbers are valid.

2. Real Constants

These constants contain fractional numbers in them. Real constants must have decimal values and they can either be negative or positive. Each real constant can be expressed as exponents also.

Example: The 23.5, 45.6756e2 numbers are valid. In 45.6745e2, the number before e is known as a mantissa and after e is known as an exponent.

3. Single Character Constants

These constants can contain only a single character in single quotes. The single character is basically a character data type available in java.

Example: The ‘5’, ‘a’, ‘45’ are valid single character constants whereas ‘ab’, ‘f5’, name are invalid single character constants.

4. String Constants

String constants contain an order of characters enclosed in starting and ending double-quotes. Characters can include numbers, special characters, spaces, and alphabets.

Example: The “String”, “.. ”, “456 ” are valid string constants.

5. Backslash Character Constants

There are some specific backslash character constants defined in java. Like ‘\t’ is known as a horizontal tab and it can be used to provide spaces at the output. Some more backslash character constants are ‘\n’, ‘\f’, ‘\b’.

This was all about constants and their types. I hope your understanding of constants has increased after this blog. If you still have any question’s you can ask them in the comments.

Leave a Reply

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