Variables And Constants in Java

Posted By Dilshad Ahmad | 26-May-2018

VARIABLES :

1. Variables are the container which holds user data. 

2. Memory will be allocated for the variable while executing the program.

3. Value of the variable can be changed any number of times during the program execution.

Syntax : 

<Data type> <varName>;

<Data type> <varName> = <value>;

Types of variables There are two types of variables based on data type used to declare the variable. 

1. Primitive Variables

2. Reference Variables

Primitive Variables Variable declare with primitive data types are called as primitive variables. Memory allocation for primitive variables depends on the primitive data types.

Default value for primitive variables depends on primitive data types. Primitive variables hold valid literals or value in the allocated memory.  

Example : 

int a;

int b = 99;

double d1;

double d2 = 9.9;

Reference Variables Variables declared with user defined data types are called as reference variables. Always 8 bytes of memory will be allocated for reference variables. Always null will assigned as default value for reference variables. Reference variables hold either null or address of an object in the allocated memory.  

Example : 

String str1;

String str2 = "Dilshad Ahmad";

Types of variables based on scopes : There are three types of variables based on scopes of the variables.

1. Instance Variables

2. Static Variables

3. Local Variables

Instance Variables : Variable declared in the class without using static keyword are called as instance variables.

Static VariablesVariables declared in the class using static keyword are called as static variables.

Local Variables : Variables declared in the member of the class like method etc are called as local variables.

class Dilshad{
   int a;           // instance variable
   static int b;    // static variable
   
   void show(){
      int c = 99;   // local variable
   }
}

CONSTANTS : 

Constant is a special variable whose value can not be modified during the program execution. Constant is also called as final variable.

Syntax 

[modifier] final <data type> <varName> = <value>;

Example

final int A = 99;

final String STR = "Dilshad Ahmad";

  • A variable that is declared as final and not initialized is called as blank final variable.

      Syntax : 

      [modifier] final <data type> <varName>;

     <varName> = <value>;

     Example :

     final int A;

     A = 99;

  • If the variable is declared as final and initialized in the same statement then in the class file compiler will replace that variable with actual value.
  • JVM will not provide default value for local variables.
  • JVM will provide default value for instance and static variables.
  • Static variables can be accessed from static context.
  • Instance variables can not be accessed directly from static method.
  • Local variables must be initialized before using.
  • Default value for char is either ASCII - 0 or UNICODE \u0000.
  • Default value for char is not space.
  • We can not declare two variables with same name in same scope. 

Thanks

Request for Proposal

Recaptcha is required.

Sending message..