Constants and Variables in Java
What is the difference between Constants and Variables used in Java?
Constants are those variables whose value cannot be changed until its scope of usage is exhausted. But we can use a keyword or modifier to control its usage.
Static modifier and final modifier can be used just to restrict is usage.
We will discuss the static and final keyword later in this tutorials but for now, just look into its syntax on using it.
public class Sample { public static void main(String args[]) { static int a = 10; final int b = 10; } }
Variables are those identifiers defined by the programmer in which the programmer can assign values according to requirement.
Also Variables are of two kinds:
- Local Variables
- Global Variables (No such concept of a global variable is used in Java)
public class Sample { public void Simple() { int b = 10;//A simple usage of local variable in JAVA b = b + 10; System.out.println("Simple = " + b); } public static void main(String args[]) { Sample a = new Sample(); a.Simple(); } }