Page 46 - CA_Blue( J )_Class10
P. 46

Defining Constants in Java
              Syntax:
                     static final <data_type> CONSTANT_NAME = <value>;
              Example:
                     public class ConstantsExample {

                         // Defining a constant
                         static final int MAX_STUDENTS = 50;
                         public static void main(String[] args) {
                            // Accessing the constant
                               System.out.println("The max. number of students is: " +MAX_STUDENTS);
                         }
                     }

                   3.7 DECLARATION VS INITIALIZATION
              Declaration of a variable in a computer programming language is a statement used to specify the variable name and
              its data type. Initialization is the process of assigning a value to the Variable.
              [Note: Assignment is simply the storing of a value to a variable.]
              Example:       int a; // simple declaration

                             a = 10; // simple assignment

              3.7.1 How to Initialize Variables in Java?
              In Java, initial default values are not assigned to local variables when they are created, and before using a local variable,
              the compiler checks whether it is assigned or not. You can initialize a variable in the same line that declares it. To do
              that you should follow this general form: type name = expression;
              Some examples are:

                 double d = 4.5;
                 String n= "India";
                 int a = 5;
                 char c='a';
              Here we are declaring and assigning the variable in a single statement. To declare more than one variable in one
              statement, each variable should have a value of its own. Example:
                 float i = 45, j = 12;
              Types of Initialization
              There are two types of initialisation: static initialization and dynamic initialization.

              Static Initialization
              Direct assignment of a constant to a variable is called static initialization. At the time of its declaration, the variable is
              initialized. Some examples are:

                                  Data types                 Declaration              Static Initialization
                          int                        int a;                      a = 20;
                          float                      float f;                    f=20.0f;
                          double                     double d;                   d= 20.0;
                          char                       char c;                     c= 'a';
                          String                     String s;                   s= "India";
                          boolean                    boolean b;                  b= flase;


                4444  Touchpad Computer Applications-X
   41   42   43   44   45   46   47   48   49   50   51