Java notes every day

Source: Internet
Author: User

Java notes every day

Generation and use of objects

Constructor is used to construct an instance of this class. the Java language calls constructor through the new keyword to return the instance of this class. The constructor is the fundamental way for a class to create objects. Without the constructor, the class cannot create instances. Therefore, if no constructor is written, a constructor is provided by default.

The constructor is a special method. The syntax format of the definition constructor is similar to that of the definition method. The syntax of the definition constructor is as follows:

[Modifier] constructor name (parameter list ){

// The constructor execution body consisting of 0 or more executable statements

}

The syntax format of the constructor is described as follows:

Modifier: the modifier can be omitted or one of the public protected private.

Constructor name: the constructor name must be the same as the class name.

Parameter list: the format of the parameter list is the same as that of the definition method.

The constructor neither defines the return value type nor uses void to define the constructor's return value. If the constructor defines the return value type or uses void to declare that the constructor has no return value, no errors will occur during compilation. However, Java treats this so-called constructor as a method for processing.

Public class Person {

Public String name;

Public int age;

Public void say (String content ){

System. out. println (content );

}

}

If there is no constructor in the Person class code, the system will provide a default constructor for it. The constructor provided by the system has no parameters.

The fundamental way to create an object is the constructor. You can call the constructor of a class through the new keyword to create an instance of this class:

// Define a Person-type variable

Person p;

// Call the Person class constructor with the new Keyword and return a Person instance

// Assign the Person instance to the p variable

P = new Person ();

// The preceding code is abbreviated as follows:

Person p = new Person ();

Object, reference, and pointer

In the preceding code, there is a line of code: Person p = new Person (); creates a person instance, also known as the person object, which is assigned to the p variable.

According to the Person class definition, the Person object should contain two member attributes.The member attributes must be stored in memory.When creating a Person object, the corresponding memory is required to store the member attributes of the Person object.

The Person object consists of multiple memory blocks. Different memory blocks store different member attributes of the Person object. What should the system do when assigning this object to another referenced variable? Will the system copy this Person object in the memory? No. The reference variable stores only a reference, which points to the actual object.

Similar to the array type described above, the class is also a type of reference data. Therefore, the Person type variable defined in the program is actually a reference and is stored in the stack memory.

The reference variables in the stack class do not actually store the member attribute data of the object, and the member attribute data of the object is actually stored in the heap memory. The reference variables only point to the objects in the memory. The pointer to the C language is very curious. It stores an address value and uses this address to reference the actual object.

This reference of the object

This keyword always points to the object that calls this method. Depending on the location where this appears, this is the default reference of an object in two scenarios:

The constructor references the object that the constructor is initializing.

The object that references this method in the method.

The biggest function of this keyword is to allow a method in the class to access another method or member attribute in the class. The following code is used:

Detailed description

Methods are the abstraction of Behavior Features of classes or objects. methods are the most important component of classes or objects. Methods in Java cannot exist independently. All methods must be defined in classes. The method is logically a class or an object.

Attribute of the Method

Methods cannot be defined independently. methods can only be defined in class bodies.

Logically, a method either belongs to the class or an object of the class.

Methods cannot be executed independently. classes or objects must be used as callers for execution methods.

The static modification method belongs to this class. The static modification method can be called by the class as the caller.

Method and parameter transfer mechanism

If you declare a method that contains a parameter declaration, you must specify the parameter values for these parameters when calling the method. The parameter values actually passed to the method are also called real parameters.

The Java method has only one parameter transfer method: value transfer. The so-called value transfer means that the actual parameter value copy is passed into the method, and the parameter itself is not affected

Recursive Method

A method called itself is called method recursion. Recursion contains an implicit loop that repeats a piece of code, but such repeated execution does not require loop control.

Calculate the value of a Series: f (0) = 1, f (1) = 4, f (n + 2) = 2 * f (n + 1) + f (n) n is an integer greater than 0. F (10)

Method overload

Java allows the same class to define multiple methods with the same name, as long as the form parameter list is different. The same class contains two or more methods with the same method name, but the form parameter list is different, it is called method overload

Caller, method owner, either a class or an object

Method Name, method ID

Parameter List. When a method is called, The system matches the list of input parameters.

Method overloading requires two different methods: the Chinese names of the same class are the same, and the parameter list is different. The type and modifier of the return value of a method, which has nothing to do with method overloading.

The two methods named test are the same, but the parameters are different. Therefore, the system can distinguish the two methods normally.

Member variables and local variables

The following fields are member attributes.

Member variables refer to the variables defined in the scope of the class, that is, the Field described above; local variables refer to the variables defined in the hair.

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.