Difference between constant and variable in java

In this article, we learn about the difference between constant and variable.

Constant:

Constant, refers to a fixed value that doesn’t change during the execution of a program. The value of constants appears right in a program. Literals is another name of constant. We use the constants to create values that assign to variables. Constants can make our program easy to read and understood by others.

Point to remember

The name of the identifier should be in capital letters that we want to declare as constant. For example,

PRICE=12.

  • As we use the private access-specifier before the constants name. At that point, we can’t change the value of the constant of that particular class.
  • And if we use the public access-specifier before the constants name. In the program, the value of the constant can be change.

How to define constant

Java does not directly support the constant. To define a variable as a constant, We use the “Static” and “Final” Keywords before declaring a variable. Scope of the constant can b local and can be global.

Constant declared as private:

import java.util.Scanner;  
public class ConstantExample1   
{  
//declaring constant   
private static final double PRICE=234.90;  
public static void main(String[] args)  
{  
int unit;  
double total_bill;  
System.out.print("Enter the number of units you have used: ");  
Scanner sc=new Scanner(System.in);  
unit=sc.nextInt();  
total_bill=PRICE*unit;  
System.out.println("The total amount you have to deposit is: "+total_bill);  
}  
}  

AS a result:

Constant declared as public:

In the above example, We declare a constant PI as public. Inside the main() method, we have assigned 3.14 in the constants PI. After that, we have invoked the printValue() method. When we execute the program, it throws an error that cannot assign a value to the final variable PI

 public class 
{  
//declaring PI as constant   
public static final double PI= 3.14;  
public static void main(String[] args)   
{  
printValue();  
//trying to assign 3.15 in the constant PI  
PI = 3.15;  
printValue();  
}  
void printValue()   
{  
System.out.print("The value of PI cannot be changed to " + PI);  
}  
}  

As a result:

Variable

A variable is an identifier that provides a storage location used to store a data value. The data stored at a particular location in memory. That we accessed by using the variable name.
A variable is a symbolic name given to a location in the computer memory. Where we store the value, which can be used in the program. The value of the variable can change during the execution of the program. It may take different values at different times during the execution of the program.
The name of the variable that we choose must be meaningful to understand, what it represents in the program.

For example

var Price = 100;

There are three types of variables in Java:

  • Local Variables
  • Instance Variables
  • Static Variables

Local Variables:

Local variables are variables that, defined within the function. The scope of this is local. This means you only can use the local variable within the functions that define them.

public class StudentDetails {
    public void StudentAge()
    {
        // local variable age
        int age = 0;
        age = age + 5;
        System.out.println("Student age is : " + age);
    }
  
    public static void main(String args[])
    {
        StudentDetails obj = new StudentDetails();
        obj.StudentAge();
    }
}

As a result:

Student age is : 5

Instance Variable:

Instance variables are non-static variables . They are declared in a class outside any method, constructor or block.

import java.io.*;
class Marks {
    // These variables are instance variables.
    // These variables are in a class
    // and are not inside any function
    int engMarks;
    int mathsMarks;
    int phyMarks;
}
  
class MarksDemo {
    public static void main(String args[])
    {
        // first object
        Marks obj1 = new Marks();
        obj1.engMarks = 50;
        obj1.mathsMarks = 80;
        obj1.phyMarks = 90;
  
        // second object
        Marks obj2 = new Marks();
        obj2.engMarks = 80;
        obj2.mathsMarks = 60;
        obj2.phyMarks = 85;
  
        // displaying marks for first object
        System.out.println("Marks for first object:");
        System.out.println(obj1.engMarks);
        System.out.println(obj1.mathsMarks);
        System.out.println(obj1.phyMarks);
  
        // displaying marks for second object
        System.out.println("Marks for second object:");
        System.out.println(obj2.engMarks);
        System.out.println(obj2.mathsMarks);
        System.out.println(obj2.phyMarks);
    }
}

As a result:

Marks for first object:
50
80
90
Marks for second object:
80
60
85

Static Variables:

Static variables are also called as Class variables. These variables are declared similarly as instance variables. The difference is that static keyword is used to declare a static variable within a class outside any method constructor or block.

import java.io.*;
class Emp {
  
    // static variable salary
    public static double salary;
    public static String name = "Harsh";
}
  
public class EmpDemo {
    public static void main(String args[])
    {
  
        // accessing static variable without object
        Emp.salary = 1000;
        System.out.println(Emp.name + "'s average salary:"
                           + Emp.salary);
    }
}

As a result:

Harsh's average salary:1000.0

Difference between constant and variable

ConstantVariables
Constant has fixed value that doesn’t change during the execution of a programThe value of the variable can change during the execution of the program
Constants are usually written in numbers.Variables are specially written in letters or symbols.
Constants usually represent the known values in an equation, expression or in line of programming.Variables, on the other hand, represent  the unknown values.
Constants are used in computer programming.Variables also have its uses in computer programming and applications.

Recommended posts

How to define constant in java

What is JavaScript?

Difference between the Java and JavaScript

Implementation of Array class in JavaScript

jQuery Introduction

Reference:

javatpoint.com

geeksforgeeks.com

Leave a Comment