Section III Java functions

Source: Internet
Author: User

1. Definition of function
in the classof havingSpecific featuresAn independent small program, also called method.Format of the function1: Modifier Returns a value type function name (parameter type parameter 1, parameter type parameter 2) {//permission access modifier: Public protected default Private, these four levels of modifiers can be used to decorate classes, methods, and fields. Executes the statement return value, and returns the value type: The data type of the result returned after the function is run. Parameter type: is the data type of the parameter. Parameter: is a variable that stores the actual arguments passed to the function when the function is called. return: Used to end the function. Return value: Returns the result value of the run to our person who called this method. The format of the function 2: modifier void function name (parameter type parameter 1, parameter type parameter 2) {EXECUTE statement return value;} When a function operation does not have a specific return value, the return type is identified by a special keyword. The keyword is void andvoid represents a function with no return value. We can omit the  return, if added is also true, do not write, the program is compiled automatically add to us.

2. Features of functions
A、定义的方法可以将该方法功能进行封装,便于将该功能反复使用
B、方法只有被调用才会被执行

3. Application of Functions

Define the method:

(1), since the function is a separate function, then the operation of the function of the result is what to be clear first. (Explicit return type of function)

(2) Whether unknown content is required to participate in the operation in the process of clearly defining the function. (Specify the parameter list for the function (the type of the parameter and the number of parameters).)

3. Overloading of functions

(1) method overloading is a means of allowing classes to handle different types of data in a uniform manner . Multiple functions with the same name exist at the same time, with different number/types of parameters.

Overloaded overloading is a representation of polymorphism in a class.


(2) Java's method overloading is the ability to create multiple methods in a class that have the same name but have different parameters and different definitions.

The method is called polymorphism by the number of different arguments passed to them and by the type of parameter to determine which method to use.


(3) When overloading, the method name is the same, but the parameter type, number and order are different, the return value type can be the same or different. The return type cannot be used as a distinguishing criterion for overloaded functions, only the parameter list .

Benefits of overloading

Easy to read and optimized for programming

Method overloading is the implementation of similar operations on data of different data types.

Method overloads differ from overrides:
Method overloading: In the same class, the name of the method is the same, but the number of arguments, the type of the parameter, or the type of the return value is different!
Method overrides: It refers to the relationship of the child class and the parent class, and the subclass overrides the method of the parent class, but the method name, parameter type, and number of arguments must be the same!


Section III Java functions

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.