Page 329 - CA_Blue( J )_Class10
P. 329

int a=7;      // Example of Primitive data type
                 Sometime, primitive data types are not fulfilling the requirement of the user. In this type of situation, Java allows you use
                 composite data types. Composite data types are those data types that are defined by the user using the primitive data
                 types. In other words, a composite data type is a collection of primitive data types. Composite data types are also known
                 as non-primitive data types. There are mainly three composite data types used in Java which are class, array and interface.

                                  int ar[] = new int[10];      // Example of Composite data type
                 The ar[] is an array containing 10 integer values. We will learn about array later in this book. Another example of
                 composite data type is String. String is a class that is a collection of char data type.


                     13.2 CLASS AS A COMPOSITE DATA TYPE
                 As you know that a class contains data members and member methods. Data members are the primitive data types
                 and member methods use these data members. Hence, a class can also be considered as a composite data type. As it
                 is defined by the user, so it also called user-defined data type. A class may be considered as a new data type created by
                 the user that has its own functionality.


                     13.3 DIFFERENCE BETWEEN PRIMITIVE AND COMPOSITE DATA TYPES
                                      Primitive Data Type                        Composite Data Type
                           It predefined data type.                   It is user-defined data type.
                           It always contains a value.                It can also contain null values.

                           It always starts with lowercase letter.    It can start with lowercase or uppercase letter.


                     13.4 INTRODUCTION TO WRAPPER CLASSES
                 Wrapper classes are the in-built classes that contain primitive data types. These classes are used to convert primitive
                 data types into objects and vice-versa with the help of their methods. Every wrapper class has various methods too
                 Thus, providing a way to use the primitive data types (short, double, boolean, etc.) as objects. Every primitive data type
                 is attached to its wrapper class. For example, the float data type is attached to the Float class, the double data type is
                 attached to the Double class, and so on.
                 All the wrapper classes are part of java.lang package in the Java library. The eight wrapper classes in java.lang package
                 are given below:
                                                Wrapper Class              Primitive Data Type

                                        Boolean                       boolean
                                        Character                     char
                                        Byte                          byte
                                        Short                         short
                                        Integer                       int
                                        Long                          long
                                        Float                         float
                                        Double                        double


                 13.4.1 Need for Using a Wrapper Class
                 Wrapper classes are used:

                 a.  To allow primitive data types to convert from one data type to another.
                 b.  To store primitive data types as objects.





                                                                                                                       327
                                                                                                        Library Classes    327
   324   325   326   327   328   329   330   331   332   333   334