Page 147 - Computer science 868 Class 12
P. 147

They are defined by the programmer according to the requirement of the program. We must use the new operator to
                 declare non-primitive data.
                 Different types of non-primitive data types are strings, classes, objects, arrays and interfaces which we will discuss later
                 in the book.

                 A class is a non-primitive data type that creates an object. As a data type is used to create a variable, similarly, a class
                 is used to create an object. So, we can say that a class is a mechanism for user-defined data types.
                 The difference between primitive data type and non-primitive data type is as follows:

                                       Primitive Data Type                       Non-Primitive Data Type
                          1.  They are built-in data types.           1.  They are used-defined data types.
                          2.  There are eight primitive data types.   2.   There are mainly  four  types of  non-primitive
                                                                        data types.
                          3.  To use them, “new” operator is not required.  3.  Use of “new” operator is mandatory.
                          4.   The size of a primitive type depends on the data  4.  The size of a non-primitive type may vary.
                            type.
                          5.   The primitive data types store only a single value. 5.   The non-primitive data types are used to store a
                                                                        group of values.

                     5.2  ARITHMETICAL EXPRESSIONS
                 Any meaningful statement containing identifiers, literals and arithmetical operators which produce some result is
                 called an arithmetical expression.

                 For example: a+b, 22.0/7.0*radius*radius

                 When an arithmetical expression is assigned to a variable, then it is called an arithmetical statement.
                 For example: c=a+b, circumference=22.0/7.0*radius*radius

                 There are two types of arithmetic expressions:
                 1.  Pure Arithmetic Expression: In this type of expression, the same types of data values are used in calculations.
                   For example:
                        int a = 6, b = 4, c;
                        c = a*b;
                        System.out.println(c);
                   Output: 24
                   The final output is in int.
                 2.  Mixed Arithmetic Expression: When an expression contains different types of data values to yield the desired
                   result, then the expression is known as a mixed arithmetic expression.
                   For example:

                        int a = 6;
                        float b = 4.5f;
                        double c;
                        c = a+b;
                        System.out.println(c);
                   Output: 10.5
                   The final output is converted into double data type as ‘c’ is a double type value.
                 From the above examples, we have observed that one type of data value can be converted into another data type
                 easily. This is called typecasting. Let us learn more about it.



                                                                                                                       145
                                                                         Primitive Values, Wrapper Classes, Types and Casting  145
   142   143   144   145   146   147   148   149   150   151   152