What is a constant and how to define constants in Java?

A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants.

A constant can make our program more easily read and understood by others. In addition, a constant is cached by the JVM as well as our application, so using a constant can improve performance.

To define a variable as a constant, we just need to add the keyword "final" in front of the variable declaration.


final float pi = 3.14f;

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. Later if we try to do that by using a statement like "pi=5.25f", Java will throw errors at compile time itself. It is not mandatory that we need to assign values of constants during initialization itself.

In the below example, we can define the primitive data type (byte, short, int, long, float, double, boolean and char) variables as constants by just adding the keyword "final" when we declare the variable.


Live Demo

public class ConstantsDemo {
   public static void main(String args[]) {
      final byte var1 = 2;
      final byte var2;
      var2 = -3;
      final short var3 = 32;
      final short var4;
      var4 = -22;
      final int var5 = 100;
      final int var6;
      var6 = -112;
      final long var7 = 20000;
      final long var8;
      var8 = -11223;
      final float var9 = 21.23f;
      final float var10;
      var10 = -121.23f;
      final double var11 = 20000.3223;
      final double var12;
      var12 = -11223.222;
      final boolean var13 = true;
      final boolean var14;
      var14 = false;
      final char var15 = 'e';
      final char var16;
      var16 = 't';
     // Displaying values of all variables
      System.out.println("value of var1 : "+var1);
      System.out.println("value of var2 : "+var2);
      System.out.println("value of var3 : "+var3);
      System.out.println("value of var4 : "+var4);
      System.out.println("value of var5 : "+var5);
      System.out.println("value of var6 : "+var6);
      System.out.println("value of var7 : "+var7);
      System.out.println("value of var8 : "+var8);
      System.out.println("value of var9 : "+var9);
      System.out.println("value of var10 : "+var10);
      System.out.println("value of var11 : "+var11);
      System.out.println("value of var12 : "+var12);
      System.out.println("value of var13 : "+var13);
      System.out.println("value of var14 : "+var14);
      System.out.println("value of var15 : "+var15);
      System.out.println("value of var16 : "+var16);


value of var1 : 2
value of var2 : -3
value of var3 : 32
value of var4 : -22
value of var5 : 100
value of var6 : -112
value of var7 : 20000
value of var8 : -11223
value of var9 : 21.23
value of var10 : -121.23
value of var11 : 20000.3223
value of var12 : -11223.222
value of var13 : true
value of var14 : false
value of var15 : e
value of var16 : t

Updated on: 14-Sep-2023

26K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started