Page 59 - iPlus_Ver_2.0_class_8
P. 59

Non-Primitive Data Types

                 The non-primitive data types are derived from primitive data types. The non-primitive data types
                 are also known as reference data types. Some of the examples of non-primitive data types are
                 class, interface, and array.
                 Literals

                 A literal denotes a constant value. Java has various types of literals. They are as follows:

                 •   Character Literal: A character literal is enclosed in single quotes and must have exactly one
                    character. For example, ‘a’ is a character literal that means the letter a.
                 •  String Literal: A string literal is always enclosed in double quotes. For example, “Hello World.”

                 •  Integer Literal: An integer literal is any number without a fraction. For example, 10 and 012
                 •   Floating-Point Literal: Any number with decimal points, like 3.12, is treated as a floating-point
                    literal.

                 •  Boolean Literal: Boolean literals can have either true or false values. They do not correspond
                    to 0 or 1 values as in C/C++.
                 Variables

                 Variables are the memory locations used to store values. When a variable is created, some space
                 is allocated for it in the memory. This memory space is referred to by the name that we give to
                 the variable. It is easy to create a variable in Java.

                 Declaring a Variable

                 In Java, a variable needs to be declared before use. Declaring a variable involves two steps: giving
                 the variable a name and stating what type of data is to be stored in the variable.
                 For example,

                        int age;
                        String name;
                        float temp;
                        double price;
                 Here, four variables named age, name, temp, and price are declared with the int, String, float, and
                 double data types, respectively.

                 Initialising a Variable

                 After declaring a variable, we need to initialise it. Intitialising means assigning a value to the
                 variable. We can initialise variables in the following way:
                        int age = 30;
                        String name = "Nisha";

                        float temp = 35.7f;
                        double price = 625.50;
                 Here, 30, “Nisha”, 35.7, and 625.50 are the values assigned to the variables age, name, temp, and
                 price, respectively.



                                                                                                                  57
                                                                                               Program Coding
   54   55   56   57   58   59   60   61   62   63   64