Java basics-java method definition

This article mainly describes the definition of java method and four types.
1. Introduction
Method is an orderly combination of codes used to solve a type of problem, which can be understood as a functional module.
Methods in java can only be created as part of a class.
2. Syntax
(1) Structure
1. Basic composition of
method Basic composition of method: return value, method name, parameter, method body, its basic form is as follows:
[Access modifier] Return value type method name (parameter list) { Method body }


a. Access modifier can be absent
. b. Method signature = method name + parameter list, which uniquely identifies a method.
c. Lower case initials of general method names and variable names, using camel case identification . (This article is supplemented and will be corrected in other subsequent articles).

2. Example
In java, main is the entry point of the program (shortcut is, input main, alt+\), we define one-to-one correspondence with the main method:
Insert picture description here

(2) Detailed structure explanation
1. Access modifier -the permission scope of the method allowed to be accessed, which can be omitted. The details are as follows:
(1) No modifier: the access authority is "package access authority" (some data is indicated as friendly)-all other classes in the current package have access authority to that member, and to all other than this package This member of the class is private;
additional explanation:
when two files are in the same directory and no package name is set by themselves (the two files four_type and null_test in the figure below belong to Exercise_base, and there is no package on them), java Such files are automatically regarded as belonging to the default package of the directory, that is, all other files in the directory are provided with package access rights :
Insert picture description here
Insert picture description here

Package access permissions provide meaning and reason for grouping classes in a package.
(2) Public: Indicates that the method can be called by any code.
private: Only the class that can contain the member can be used, even if it is inherited, it cannot be used.
Insert picture description here
Protected: It is open to inheriting one's own class, base class, and classes in the same directory, without any restrictions, and is equivalent to private to other external classes.
(3) The scope list of the three modifiers
Insert picture description here

2. Return value type -the type of return value from the method after calling the method
(1) No return value: designated as void
Description:
a. No return keyword: the method needs to be executed to the end.
b. There is a return keyword: make the program end flexibly (return can be added where you want to exit, generally used in conjunction with the judgment statement if).
Insert picture description here
(2) There is a return value: the return value type needs to be specified, and the return statement is used in the method body, and the return value type must be correct .
The function of the return keyword
a. It stands for "the method has been executed and will leave this method";
b. The method produces a value that is the value after return

3. Method name To
define the method name, legal identifiers
are required. The following identifiers cannot be used:
abstract, assert, boolean, break, byte, case, catch, char, class, continue, default, do, double, else, enum, extends, final, finally, float, for, if, implements, import, int, interface, instanceof, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while
4. Parameter list -give the type of information to be passed to the method and the method
parameter list is to specify what kind of information to pass to the method, there can be multiple parameters (Use commas to separate), each parameter is composed of type and name, separated by spaces.
5. Method body
Mainly reflects the realization process of the method.
3. Method invocation (this behavior is called: sending a message to the object) The
method can only be invoked through the object. If the method called by the object does not exist, an error will be reported at compile time. The calling method is as follows:
object name. method name (parameter 1, parameter 2, ...)
The parameters passed in the function call in java follow the principle of value transfer ( the value of the data itself when the basic type is passed, and the reference type is passed A reference to the object , not the object itself).
eg:
int sum =A.sum(a,b)

Guess you like

Origin blog.csdn.net/qq_44801116/article/details/105095061