Java One-day note

Source: Internet
Author: User

The generation and use of objects

The constructor is used to construct an instance of the class, and the Java language invokes the constructor with the new keyword to return the class instance. A constructor is a fundamental way for a class to create an object, and a class cannot create an instance without a constructor. Therefore, if you do not write a constructor, a constructor is provided by default.

The constructor is a special method that defines the syntax format of the constructor as much as the syntax format of the defined method, which defines the syntax of the constructor as follows

[modifier] Constructor name (formal parameter list) {

A constructor actuator consisting of 0 or more executable statements

}

A detailed description of the constructor syntax format is as follows:

Modifier: modifier can be omitted, or it can be public protected private one of them

Constructor Name: The constructor name must be the same as the class name

Formal parameter list: identical to the format of the definition method parameter list

The constructor can neither define a return value type nor use void to define a constructor with no return value. If the constructor defines a return value type, or uses void to declare that the constructor does not return a value, compile without error, but Java treats the so-called constructor as a method

public class Person {

public String name;

public int age;

public void Say (String content) {

SYSTEM.OUT.PRINTLN (content);

}

}

There is no constructor in the person class code, and the system will provide him with a default constructor, and the system-supplied constructor is without parameters.

The fundamental way to create an object is the constructor, which invokes the constructor of a class with the new keyword to create an instance of the class:

Define a variable of type person

Person p;

Call the constructor of the person class with the new keyword to return a person instance

Assign the person instance to the P variable

p = new person ();

The above code is abbreviated as follows

Person p = new person ();

objects, references, and pointers

In the preceding code, there is a line of code: Person p = new person (), a person instance is created, also called a person object, the person object is assigned to the P variable

From the person class definition, the person object should contain two member properties, and the Membership attribute requires memory to be stored. When you create a person object, you must have the corresponding memory to store the member properties of the person object

The person object consists of multiple pieces of memory, each of which stores different member properties of the person object. When assigning this object to another reference variable, how does the system handle it? Does the system make a new copy of this person object in memory? No, the reference variable holds only a reference, which points to the actual object

Like the array type described earlier, a class is also a reference data type, so a variable of the person type defined in the program is actually a reference, which is stored in the stack memory.

The reference variable from the stack class does not really store the object's member property data, the object's member property data is actually stored in the heap memory, and the reference variable simply points to the object in that memory. With the C language of the pointer, is to store an address value, through this address to refer to the actual object

The This reference of the object

The This keyword always points to the object that called the method. Depending on where this occurs, this is the default reference for an object in two scenarios:

The object that the constructor is initializing is referenced in the constructor

The object that references the method in the method

The most important role of the This keyword is to have a method in the class that accesses another method or member property in the class. As shown in the following code:

Detailed method

A method is an abstraction of the behavior characteristics of a class or object, which is the most important part of a class or object. Methods in Java cannot exist independently, and all methods must be defined in the class. Methods logically belong to a class or belong to an object.

The ownership of the method

Methods cannot be defined independently, methods can only be defined in the class body

Logically, the method either belongs to the class itself or belongs to an object of that class

You can never execute a method independently, and the execution method must use the class or object as the caller

The static adornment method belongs to the class itself, and the static decorated method can be invoked using the class as the caller.

Method and parameter passing mechanism

If you declare a method with a formal parameter declaration, you must specify the parameter values for the parameters when the method is called, and the parameter values that are actually passed to the formal parameter are also called arguments

There is only one way to pass the parameters of a Java method: value passing. Value passing means that a copy of the actual parameter value is passed in to the method, and the parameter itself is unaffected

Recursive method

A method body calls itself, called method recursion. Recursion contains an implicit loop that repeats a piece of code, but this repetitive execution does not require loop control

Computes the value of the sequence: F (0) =1,f (1) =4,f (n+2) =2*f (n+1) + f (n) where n is an integer greater than 0. Ask F (10)

Method overloading

Java allows a class to define multiple methods with the same name, as long as the parameter list is different. Method names that contain two or more than two methods in the same class have the same name, but different parameter lists are called method overloads

Caller, method owner, can be either a class or an object

Method Name, Method's identity

Parameter list, when the method is called, the system will match the incoming argument list

Method overloads require two identical differences: the same method name in the same class, and a different argument list. method returns a value type, modifier, and so on, with no relation to the method overload

Test two method names are the same, but the parameters are different, so the system can distinguish between the two methods normally

member variables and local variables

The following field is a member property

A member variable refers to a variable defined in a class scope, which is the field described earlier, and a local variable refers to a variable defined in that hair.

Java One-day note

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.