Page 149 - Computer science 868 Class 12
P. 149

The order in which the explicit conversion can be done is as follows:

                       byte          short         char           int           long          float        double


                 In the above order, the right-side data type is converted into the left-side data type in the explicit conversion.

                 Let us take an example for better understanding.
                    int a = 6, c;
                    double b = 4.5;
                    c = (int)(a+b);
                    System.out.println(c);
                        Output: 10

                 The mechanism of explicit conversion in the above example is as follows:

                             c  =  (int) (a  +  b   ;                 Here,  there are two  different types of  data

                                                                      types. One int and another double. By default,
                                                                      the added value will be converted into double.
                                                            double    But  with  user’s intervention  in  the form of
                                                            int       (int), the result will be converted into integer

                                                            int       data type and stored in variable c.


                       Note:  boolean data type is not compatible with any other primitive data type. So, it cannot be converted
                       into any other type or vice-versa neither by implicit nor explicit data type conversion. If tried to be
                       converted, it returns an error, e.g., “incompatible types: char cannot be converted to boolean”.



                     5.4  WRAPPER CLASS
                 A Wrapper class in Java is a type of class that is used to convert primitive data types into objects and objects to
                 primitive data types. It wraps around a variable of one data type and converts it into an object. Thus, a wrapper class
                 provides a way to use the primitive data types (short, double, boolean, etc.) as objects in Java.
                 Two primary uses of Wrapper Class are:
                 •  Converts a String value/Object into its primitive type and vice-versa.
                 •  Objects can store primitive values.

                 Wrapper classes are included in Java in java.lang library package. The java.lang package contains eight wrapper classes
                 that are listed below.

                                      S. No.         Primitive Data Type            Wrapper Class
                                       1.      boolean                      Boolean
                                       2.      char                         Character
                                       3.      byte                         Byte
                                       4.      short                        Short
                                       5.      int                          Integer
                                       6.      long                         Long
                                       7.      float                        Float
                                       8.      double                       Double



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