Friday, May 20, 2016

Variable

Just as in other programming languages, in Java, variable is also the basic unit needed to store a value to a specific data type in the computer's memory. Every variable has a scope and the time to live in the memory.


Variable Declaration


Here is the general form to declare variables in Java:

type variableName;   //to declare a variable
type variableName1, variableName2;   //to declare several variables

For example, if we want to declare a variable with name x and data type int, then we should write it as follows:

int x;

You can also declare some variable with the same type at once by using commas as its separator, as shown in the code below:

int x, y, z;   //declare variable x, y, z with int type
char ch1, ch2   //declare variable ch1, ch2 with char type

There are some rules to be considered in determining the variable name at the time of the declaration process, which is as follows:
  • Variable name can not contain spaces.
  • Variable name must not be preceded by a number or numeric characters (digits).
  • Variable name must not contain the symbol, except for the $ sign.
  • Variable name must not contain the keywords that have been defined in Java salutation, eg: while, break.
  • In a program block, the variable name must be unique or different from one another.
  • Keep the variable names as descriptive as possible so that the program code can be more easily understood by others (especially when working with the team).

In Java, variable names always begin with a lowercase letter, for example: hairColor, isExist, and so forth. This also applies to the method name in a class, for example: toString(). We will discuss about the method on next post.


Variable Initialization


Java allows us to initialize the value in variable declaration. The initial value is the value used as the initial (default) before finally variable is filled/replaced with another value. The general form of variable Initialization is as follows:

type variableName = value;
type variableName1 = value1, variableName2 = value2;

Here are two examples of code that demonstrates the variable Initialization:

int index = 0;
char ch = 'A';

The above code shows that we declare a variable named index and type int with 0 as initial value, and a variable named ch and type char with 'A' as initial value.


Scope and Life Cycle of Variable


Variables declared within the scope or a particular block will only be recognized within the scope of the relevant course. In Java, a block begins with { and end with }. Consider the following code examples:

class variableScope {
   public static void main(String[] args) {
      int a = 10;

      if(a>5) { //start of block
          int b = 15;
         System.out.println("a value in if block: " + a);
         System.out.println("b value in if block: " + b);
      }  //end of block

      System.out.println("a value outside if block: " + a);
      //false
      //System.out.println("b value outside if block: " + b);
   }
}

The following result will be displayed on the screen:

a value in if block: 10
b value in if block: 15
a value outside if block: 10

In the code above, b variable declared inside if block that would not be recognizable from outside the block.

2 comments:

  1. Thank you for sharing valuable information with us.

    Java web application is a type of free programming language used to develop applications and software that are becoming highly in demand on the internet. The software engineer can provide the web application or software in the form of windows, Linux, and MAC OS. There are various types of systems that can not work without java and it is getting necessary day by day even the world’s no.1 application depends on java. Java has become a most popular feature in android smartphone applications that's why Aptech learning provides java training in Dwarka, Janakpuri, and in Gurgaon also. Java training helps to get you complete knowledge about web frameworks, Java testing tools, and design patterns in java.

    Let's get learn about the PHP training course
    PHP is a type of programming language that is most commonly used in dynamic functions such as modifying, collecting, and deleting all types of databases It also helps to encrypt all types of data in the form of HTML and XML file. Aptech learning provides you with a complete PHP training course at a reasonable cost that will help you to grow more in the era of the software industry.

    For more details please visit our website : Aptech Janakpuri

    ReplyDelete