Page 253 - IT-802_class_12
P. 253

Similar to the Integer wrapper class for the int datatype, each of the eight primitive types has a wrapper class defined
            for it (in the java.lang package) all of which are imported by default in Java programs. Table given below lists the
            wrapper classes for the corresponding datatype.


                                 Primitive Datatype                             Wrapper Class

                    boolean                                      Boolean

                    byte                                         Byte
                    char                                         Char

                    int                                          Int

                    float                                        Float

                    double                                       Double

                    long                                         Long
                    short                                        Short


              3.12 cLaSS dESIgn

            We are now ready to create a class in Java. A class in Java begins with the keyword class followed by the name of the class.
            The body of the class is enclosed within curly braces. The body contains the definitions of the data and method members
            of the class. The data members are defined by specifying their type. The method members of the class are defined just
            like the user defined methods we saw earlier. The method members have access to all the data members of the class and
            can use them in their body. The data members of a class are like global variables – they can be accessed by all the method
            members of the class.
            How to code the class Book in Java is shown below:

            public class Book
            {
              String title;
              String author;

              String publisher;
              String genre;
              double price;
              void display()

              {
                System.out.printin (“Title “+title);
                System.out.printin (“author “+author);
                System.out.printin (“publisher “+publisher);

                System.out.printin (“genre “+genre);
                System.out.printin (“price “+price);
              }
            }



                                                                               Fundamentals of Java Programming  251
   248   249   250   251   252   253   254   255   256   257   258