Java Basic Learning Notes--11 (methods, overloads, recursion)

Source: Internet
Author: User

1. Methods

1) Method Effect:

Encapsulates a specific logic function to make the program structure clear and easy to reuse code

2) Definition of the method:

The main three elements of the method are: The method name, the parameter list, and the return value.

Modifier return value type method name (parameter list) {

Method body

}

Description

① return value type:

Method is used when returning a result, no return value is void, and a return value is designed to be a specific data type.

② parameter list: parameter design, make the method processing more flexible, parameter according to demand is dispensable.

3) method Invocation:

Depending on whether there is a return value or if there are no arguments, and the invocation condition, the following method is called

① method Name ();

② method Name (parameter value);

③ Data Type variable = method name ();

④ Data Type variable = Method Name (parameter value);

The ⑤ object. method Name ();

⑥ object. Method name (parameter value);

⑦ data type variable = object. Method Name ();

⑧ data type variable = object. Method name (parameter value);

Case 38:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M01/6D/31/ Wkiom1vdwo6bxqnbaaljzng6hyo277.jpg "alt=" Wkiom1vdwo6bxqnbaaljzng6hyo277.jpg "/>

2. Overloading of methods (overload)

The signature of the method includes the following two aspects: The method name and the parameter list. Java syntax stipulates that a class can not have two methods to sign the exact same method, that is: a class can not have two methods of the method name and parameter list are identical, but if a class of two methods are only the same method name and the parameter list is different, it is possible.

* * The overloads of the method are independent of the return value type.

Case 39:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M01/6D/2C/ Wkiol1vdxcjh6i-qaaibtkhpi8g853.jpg "alt=" Wkiol1vdxcjh6i-qaaibtkhpi8g853.jpg "/>

3. Transfer of parameters

The Java programming language has only value-passing parameters. When an object instance is passed as a parameter to a method, the value of the parameter is a copy of the object's reference. Points to the same object, the object's contents can be changed in the called method, but the object's reference is never changed.

If the parameter type is a basic type, then a copy of the parameter is passed in, which is the value of the original parameter, which is the same as the previous value. If you change the value of a copy in a function, the original value is not changed.

If the parameter type is a reference type, then a copy of the reference parameter is passed in, and the copy holds the address of the parameter. If you do not change the address of the copy in the function, but instead change the value in the address, the changes within the function will affect the parameters passed in. If you change the address of a copy in a function, such as new, the copy points to a new address, and the passed in parameter points to the original address, so the value of the parameter is not changed.


4. Recursion

Recursive invocation: Method calls the method itself, with a recursive solution to the problem is more concise, just consider a layer of logic can! Be sure to have an end condition (such as: f (1) = 1;).

The idea of recursion is the recursive thought in mathematics. For example: Factorial 4! = 4*3*2*1, 3! = 3*2*1, 2! = 2*1, 1! = 1; Must have a terminating value.

The recursive hierarchy cannot be too deep. In short: Use recursion with caution!

Case 40:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M02/6D/2C/ Wkiol1vdxe6hleynaahmrwdzzmk841.jpg "alt=" Wkiol1vdxe6hleynaahmrwdzzmk841.jpg "/>

Case:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M02/6D/31/ Wkiom1vdwuryck8baae8hrxymge538.jpg "alt=" Wkiom1vdwuryck8baae8hrxymge538.jpg "/>

This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1653620

Java Basic Learning Notes--11 (methods, overloads, recursion)

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.