Java notes 15. In-depth understanding of classes and objects (2)

Source: Internet
Author: User

A class is a description of a type of transaction, an abstraction, a conceptual definition, an object that is actually an individual of that type of transaction, and, therefore, an instance. It is visible that a class describes the properties of an object and the behavior of an object, and a class can correspond to multiple objects. first, the object 1.new Keywords in Java programming, we instantiate an object of a class by using the New keyword and the class name of the object that you want to create. instantiating an object is allocating memory for an object, and the new operator determines how much memory the new object allocates to store the object , depending on the construction method . The new operator requires a parameter, which is how the class is constructed, and the constructor is a special method for initializing the object. The new operator allocates memory for an object and invokes the constructor of the class to determine the initial state of the object (that is, assigning an initial value to the member variable of the class). Examples are as follows: Person p1=new person (); where the variable P1 is the reference handle to the object, the object's reference handle is A variable allocated in the Stack, and the object itself is allocated in the heap . 2. Comparison of objectswe've just talked about how to create an object, but often there are comparisons between objects in the same type of object in development. Java provides two methods for comparison between objects:the "= =" operator and the Equals () method . (1) "= =" operatorA comparison of the Basic (Reference) data type variables is understood to compare the values of two variables for equality . (2) Equals () methodThe Equals () method is a member method of the string class that compares the contents of the objects pointed to by two reference variables for equality. such as: string str1 = new String ("abc");String str2 = new String ("abc");String str3=str1;if it is determined by the "= =" operator, then: STR1!=STR2,STR1==STR3. Because Str1 and str2 point to two newly created string class objects, they are two separate objects, two objects that occupy different memory space addresses. STR1 and STR2 are the handles to these two objects, respectively, the memory addresses of the two objects, and obviously their values are not equal. and    If you use the Equals () method to determine, then: STR1=STR2,STR1=STR3. Because the contents of the three objects of str1, STR2, STR3 are all "abc", they are equal to each other.
3. Anonymous Objects When you create an object, when you call the method of the object, you can also call the object's method without defining its handle, which is called an anonymous object, such as new Person (). Shout (). There are two cases of using anonymous objects: (1) If you only need to make one method call to an object , you can use an anonymous object. (2) passing an anonymous object as an argument to a function call. Example: There is a getsomeone function in the program to receive a person class object as a parameter. the function is defined as follows:Public static void Getsomeone (person p){......}As you can see, the function needs to pass a parameter to it when it is used, so we can call it this way: getsomeone (new Person ()); An anonymous object that defines a person class as an argument to the function.
second, the construction method 1. Construction MethodThe construction method is very useful in programming, it can initialize the member variables of the class, and when the instance object of a class is created, the constructor of this class is automatically called . Unlike member methods, construction methods generally have the following characteristics:(1) It has the same name as the class;(2) It does not contain the return value;(3) It cannot return a value in a method with a return statement. Note: The name of the constructed method is the same as the name of the class, because the constructor is called by the Java compiler, and the compiler must know which is the construction method. 2. Overloading of construction methodswhen we implement a class, we can not implement the construction method, because the system automatically generates an empty construction method (the method does not contain any code) for the system to call automatically. the overloads of the constructor method are implemented by passing a different list of parameters and can perform different initialization operations . When creating an entity object, which constructor is used, Java automatically chooses the appropriate constructor based on the parameters passed in parentheses, the new class name (the argument list). The following is an analysis of what a construction method does specifically:person p=new person ("Tom", 18), where p is the reference handle to the object. (1) An instance object of the person class is created;(2) allocating memory space for instance objects in heap memory;(3) invokes the constructor of the specified class, assigning the first address of the content of the instance object to the reference variable (reference handle) p. Memory AnalysisA. First, instantiate an object person p=new the person ("Tom", 18), generate a reference handle P, and the system automatically initializes the member variables in the person class.
B. Second, execute the code in the constructor method, re-assign the member variable with the name and age accepted from the outsideThis.name=name;this.age=age; The memory status at this time is:
C. Finally, assign the object you just created to the reference variable.
3. Summary of construction methods(1) There is at least one constructor in every class in Java, and if we do not define a constructor in a class, then the system automatically generates a default constructor for that class, which has no parameters and no code in its method body, that is, nothing. (2) Construction methods are generally public, because they are automatically called by the system when the object is produced, so they can be called externally. (3) in creating an object statement, such as person p=new person (), where the reference variable p is the reference handle to the object, not the object itself, we can manipulate the properties (member variables) and behavior in the object by referencing the variable p ( Member method).

Java notes 15. In-depth understanding of classes and objects (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.