Page 70 - CA_Blue( J )_Class9
P. 70

The syntax for declaring a variable is as follows:

                  <data type> <name of the variable>;
                  Where, the <data type> specifies any primitive or non-primitive data type and the name of the variable should
                  be a valid  Java identifier. For example, int age;  Here,  int is  the data type and  the age is  the variable  name.
                  As you know, that every statement is terminated with the help of a semicolon. Similarly, the declaration statement
                  is also terminated by using a semicolon.

                  For example:
                  int age;

                  Here, int is the data type, and age is the variable name.
                  You can also declare multiple variables of the same type in a single statement, separating them with commas.
                  For example:

                  int age, marks;

                  4.6.2 Initialise a Variable
                  Initialisation is the assignment of a value to a variable at the time of declaration with the help of ‘=’ (equal to
                  operator). Assignment is simply the storing of a value to a variable. The assignment operator assigns the value at
                  the right side of it to the variable at the left side of it.  For example, a = 10; In Java, initial default values are not
                  assigned to local variables when it is created and before using local variable compiler checks whether it is assigned
                  or not. You can initialise a variable in the same line that declares it. To do that you should follow this general form,
                  type name = expression;
                  For examples:

                  double d = 4.5;
                  String n= "India"; (Here we are declaring and assigning the variable in a single statement.)

                  int a = 5;
                  char c=‘a’;

                  Java allows you to declare more than one variable in a one statement, then each variable should have a value of
                  its own. For example: float i = 45f, j = 12f;

                  Variables can be initialised in two ways, static initialisation and dynamic initialisation.

                  Static Initialisation
                  Assigning values to variables at the time of declaration is called Static Initialisation. At the time of its declaration
                  the variable is initialised.

                  Some examples are:

                                      Data types               Declaration           Static Initialisation
                               int                      int a;                    a = 20;

                               float                    float f;                  f=20.0 f;
                               double                   double d;                 d= 20.0;

                               char                     char c;                   c= ‘a’;
                               String                   String s;                 s= “India”;

                               boolean                  boolean b;                b= false;


                    68    Touchpad Computer Applications-IX
   65   66   67   68   69   70   71   72   73   74   75