Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 125 - 2617_JSSPS_C-7
P. 125

    DIFFERENCE BETWEEN CONSTRUCTOR AND METHOD

                                       Constructor                                            Method
                     It is used to initialize the instance variables of  It is used to execute the Java code which performs
                     the class.                                        a specific job.

                     It can be invoked only once at the time of  It can be invoked anytime as and when required.
                     creating the object.
                     It has the same name as that of the class.        It has the different name from the class.

                     It does not have a return type.                   It has a return type.
                     It always public.                                 It may be public or private.

                                Quick Quiz

                      1.  What is the purpose of the switch statement in Java?

                                 a. To replace the long if…else ladder and simplify the code

                                 b. To allow the execution of multiple conditions without stopping

                      2.  How can you use a predefined method like CharAt() in Java?

                                 a. By creating an object of the class that contains the method

                                 b. By defining your own method with the same name

                                  Recap

                                   Java is an object-oriented programming language. It was developed by James Gosling and his team
                                including Mike Sheridan and Patrick Naughton, at Sun Microsystems in 1995.
                                   A data type is an attribute of data which tells the compiler or interpreter how the programmer
                                intends to use the data or value.
                                   A variable is a name given to a space in computer memory to store a value. In other words, it is a
                                reference to a memory location where data is held.
                                   Conditional statements allow us to change the default flow of a program.
                                   Looping statements are the control flow statements that allow us to repeatedly execute a set of
                                statements for a given number of times.
                                   Object-Oriented Programming is another way of programming. It follows a Bottom-Up approach
                                and emphasises more on data rather than functions.
                                   Methods are necessary to perform operations on the data members of a class.
                                   A programmer can define a function anywhere inside a class after declaring the data members.
                                   A constructor is a type of special member method with same name as of class.

                                                                                                   Java Programming    123

| 120 121 122 123 124 125 126 127 128 129 130 |