Page 107 - CA_Blue( J )_Class9
P. 107

You will get the following output:
















                     5.4 SPECIAL OPERATORS IN JAVA
                 Java provides two special operators that are dot (.) and new.

                                                         SPECIAL OPERATORS





                                     Dot (.) Operator                              New Operator


                 5.4.1 Dot (.) Operator
                 Dot (.) operator is used to access the member of a class or a package. It is also used to access members of a
                 package. It is also known as member operator.

                 Let us see the following syntax:
                    System.out.println("We are students of class 10");
                 Here, System is a class in the java.lang package.
                 out is a static member of the System class, which is an instance of PrintStream.

                 println is a method of PrintStream that outputs text to the console.
                    "import java.util.*"
                 Here, util is a package and using dot operator, all the Classes in the package are included in the program.

                 5.4.2 New Operator
                 The new operator in Java is used to allocate memory in the dynamic memory (RAM) for storing the data members
                 and methods that belong to an object of a class. Using the new operator, we can initialize all non-primitive data
                 types, including objects and arrays.The new operator’s primary purpose is to create instances (objects) of a class.
                 It also initializes the object’s fields with default or specified values by calling the class constructor.
                 Syntax of the new operator is as follows:
                 Class_name object_name=new Constructor_of_Class();
                 The new operator is used to create the object of a class. Let us see the following syntax:

                    Average ob = new Average();
                 Here, Average is the class name, ob is the object created and Average() is the constructor of the class Sum. The
                 new operator is used here to initialize the object ob of the class Average. Thus, new operator is used to assign
                 space in the dynamic memory (RAM) to the data members and methods that are parts of an object. This operator
                 is also used to create an array:

                    int array[]=new int[5];
                 Here, an array of type int with a size of 5 is created.




                                                                                               Operators in Java   105
   102   103   104   105   106   107   108   109   110   111   112