Variable

  • Variable is a representation of memory location, which holds the value.

  • Every variable is assigned with a data type.
  • All  variables should be identified with unique names. These unique names are called identifiers. (if you want to walkthrough about identifiers. see this post previous post)

  • Variables can be short names or descriptive names (like name, sum, result). 
  • In order to use a variable in a program you have to perform 3 steps
      • Variable Declaration

      • Variable Initialization

      • Variable Utilization


Variable Declaration

  • To declare the variable the data type and give the variable (Unique name/Identifier)

  • Let us consider the variable as age, age is a whole number. So, we can use the data type from integer group (byte,short,int and long). generally, we used int because, it is the easy to use and also more efficient at run time.     



Variable Initialization

  • To Initialize the variable, we should assign the value
  • Let's take the already declared variable of age and assign the value of 25




Note : We can combine the variable declaration and Initialization
in a single line.



Variable Utilization

  • To Utilize the variable, we should use the variable name (or) identifier.

  • Let's Consider the variable of age which holds the value of 25, If you want to display the age in console. we have to use the variable name of age inside the print statement



Types of variable

    • Local Variable

    • Global/Instance Variable

Local Variable

  • Variable which is declared inside the method is called as local variable



Global / Instance Variable

  • Variable which is declared inside the class. but Outside the method is called as global variable