Page 145 - ComputerScience_Class_11
P. 145

5             short sd;
                   6             int id;
                   7             long ld = 2571;

                   8             double dd = 323.14;
                   9             System.out.print("Converting long to int: ");
                  10             id = (int)ld;

                  11             System.out.println("long ld = " +ld+ " int id = " +id);
                  12             System.out.println("---------------------------------------------");
                  13             System.out.print("Converting double to short: ");

                  14             sd = (short)dd;
                  15             System.out.println("double dd = " +dd+ " short sd = " +sd);
                  16         }

                  17     }

                 The output of the preceding program is as follows:
                         BlueJ: Terminal Window - Java

                     Options

                    Converting long to int: long ld = 2571 int id = 2571
                    ---------------------------------------------
                    Converting double to short: double dd = 323.14 short sd = 323



                        Note: The boolean data type is not compatible with any other primitive data types. So, it cannot be
                        converted to any other type or vice versa neither by implicit nor explicit data type conversion.



                     6.6 WRAPPER CLASS
                 Wrapper classes are 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. Thus, it
                 provides 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.


                 6.6.1 Corresponding Wrapper Classes for Each Primitive Type
                 The wrapper class is used to convert the primitive data type to its object and vice versa. A wrapper class is required to:
                 •  Store the primitive data type in an object.
                 •  Convert String to primitive data type and vice versa.
                 Each primitive data type has its own wrapper class. Some of the wrapper class lists are as follows.
                                            Primitive Data Type                Wrapper Class

                                     byte                             Byte
                                     short                            Short
                                     int                              Integer



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