Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 94 - 2617_JSSPS_C-7
P. 94

    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;

                  Dynamic Initialisation

                  Assigning values to variables during runtime, often based on some logic or user input called dynamic
                  initialisation. Some examples are:

                                    Data types              Declaration            Dynamic Initialisation
                                 int                int a1, a2, a3;               a1=a2 + a3;

                                 float              float i1,i2,i3;               i1=i2 + i3;
                                 String             String c= “India”, d= “Delhi”  e= c + d;
                                                    , e;

                            Premium Edition-VII
                     92

| 89 90 91 92 93 94 95 96 97 98 99 |