Page 72 - Viva ICSE Computer Studies 8 : E-book
P. 72

Variable Declaration
                    Declaration of a variable defi nes the data type and the name of a variable.

                    Syntax: <Data type><Variable name>
                    For example: int sum;

                    fl oat percentage;
                    Here, int and fl oat are the data types, and sum and percentage are the variable names.

                    We can also declare more than one variable of the same data type in a single statement,
                    separated by a comma.
                    For example: int sum, diff ;


                    Variable Initialisation
                    Initialisation means assigning a value to a variable.

                    Syntax: <Data type><Variable name = Initial value>;

                    For example: int sum = 0;

                    Here, sum is a variable to int data type and is assigned the value of 0.

                    Data Types

                    Data type refers to the type of data that can be used in a program. Java provides number of
                    data types to utilise maximum amount of memory. Some commonly used data types are as
                    follows.


                                    Data Type                       Size                      Examples
                                        int                   16 bits to 32 bits            22, 45, 89, –78

                                   fl oat, double             32 bits to 64 bits             34.78, –9.56
                                       char                        16 bits                   ‘a’, ‘A’, “Viva”

                                     boolean                        1 bit                     True, False


                    Operators

                    Operators are the special symbols used to perform calculations. Operators form an expression
                    when applied to variables and constants.
                    Example: a = b + 2

                    Here, a and b are the variables, ’2’ is a constant and ‘+, =’ are the operators. Let’s learn about
                    various operators of Java.
                    Arithmetic Operators

                    Arithmetic operators are used for various arithmetic calculations such as addition, subtraction
                    and so on. They include the following operators.





                     60
   67   68   69   70   71   72   73   74   75   76   77