Constants
info
Constants is a special variable whose value can't be modified during the program execution. Constant is also called as final variable.
Synthax:β
[modifier] final <data Type> <variableName> = <value>;
Example:β
final int a = 99;
final String str1 = "Hello World";
final double d = 99.99;
A variable that is declared as final and not initialized is called blank final variable.
Synthax:β
[modifier] final <data Type> <variableName>;
<variableName> = <value>;
Example:β
final int a;
a=99;
If the variable is declared as final and initialized in the next statement, then in the class file, compiler will replace that variable with the actual value.
SUMMARYβ
Variables & Constantsβ
- Default value of
charis either ASCII - 0 or UNICODE\u0000. - Default value of
charis not space. - JVM will not provide default value for Local Variable.
- Local variable must be initialized before using.
- JVM will provide default value for static Variable.
- Static variable can be accessed from Static method (main method).
- JVM will provide default value for instance Variable.
- Instance variable cannot be accessed directly from Static method (main method).
- You canβt declare multiple variables of different data types in single variable declaration statement.
- You can declare multiple variables of same data types in a single statement.
- You can assign same value to multiple variables in a single statement.
- You can declare and initialize multiple variables of same data type in a single statement.
- We canβt declare two variables with same name in same scope.
- Value of the variable can be changed any number of times during program execution.
- Value of the final variable can not be changed.
- We canβt use
constkeyword to declare constant.