Page 137 - ComputerScience_Class_11
P. 137

We know that a variable is a named memory location where a value is being stored. The name of the variable contains
                 any combination of letters without any space.
                 But we should remember that it must not start with any digit. If the name is a combination of two or more words, then
 Primitive valueS, wraPPer   we can use an underscore (_) to separate the words.
                 For example:
 claSSeS, tyPeS aNd caStiNg
                   int a = 5; double sum_of_numbers = 40.5;
                 6.1.2 Literals
                 The values that are stored in the variables are known as literals. These values can be of any data type.

                   int a = 10;
                   double d = 11.55;
                   String s = "India is a country";
                 Types of Literals
                 There are six different types of literals. They are integer literals, floating-point literals, character literals, string literals,
                 boolean literals and null literals. Let us learn about them in detail.
                 •   Integer literals: Integer literals are the whole numbers of any length, without any fractional part. They may be
                   positive, negative or zero. For example, 78, +456, -765, etc.
                 •   Floating-point literals: Floating-point literals are the fractional or real numbers. They are also called real literals.
                   They may be positive or negative. For example, 2.65, 985.94, -5.68, etc.
                 •   Character literals: Character literals are the alphabets, numbers or symbols enclosed in single quotes. For example,
                   ‘B’, ‘^’, ‘5’, etc.
                 •   String literals: String literals are a sequence of characters that are enclosed in double quotations. For example,
                   “India”, “Kolkata 700115”, “I am an Indian”, etc.
                 •   Boolean literals: Boolean literals represent logical values. They can have only two values: true or false.
                 •   Null literals: Null literals represent the null value in a string variable when we need to initialise it. The default value
                   is ‘null’.

                 6.1.3 Assignment
                 Assigning a value to a variable using the symbol “=” is known as an assignment. This “=” is an assignment operator.
                   int a = 5;          // here 5 is assigned to the variable a
                   double b = 6.7;  // here 6.7 is assigned to the variable b
                 Initialisation
                 Initialisation is the assignment of a value to a variable at the time of declaration.

                 Types of Initialisation
                 There are two types of initialisations. They are static initialisation and dynamic initialisation. As soon as a variable is
                 declared, it contains garbage values. This garbage value has to be removed, as it may lead us to a wrong result.
                 •  Static initialisation: When a value is directly assigned to a variable at the time of declaration, it is known as static
                   initialisation. For example,

                       int a=6;
                       String n="Name";
                 •  Dynamic initialisation: When a variable is initialised during the runtime of the program, it is known as dynamic
                   initialisation. For example,
                       int a=5, b=6, c;           // Known as Static Initialisation
                       c=a+b;                     // Known as Dynamic Initialisation
                       System.out.println(c);


                                                                      Primitive Values, Wrapper Classes, Types and Casting  135
   132   133   134   135   136   137   138   139   140   141   142