Definition of the 22.java method

Source: Internet
Author: User
Tags modifier

Methods in Java: Equivalent to the functions in C language;
In developing Java, Sun introduced a method to increase the reuse of its code.


What is a method?
The method is a piece of code that can be used to accomplish certain functions and can be reused.
Start the call from the entry program and start executing the specified method as soon as you drop it.


Syntax for defining methods:
The return value type method name of the [method's modifier list] method {

Java statements;
}

Note: [] inside the brackets can have or not, but not in the [] there is a certain.
1) [Modifier list of methods] is optional and is now temporarily written as: public static
2) The return value type of the method, which can be any of the data types in the Java language, including the base data type and the reference data type.
3) If there is no return value after the execution of the method, the return value type is written when the method is defined: void
4) The method name can be used as long as it is a valid identifier
5) The formal parameter list of the method, which can have parameters or no parameters, separated by a "comma" if there are multiple arguments.

public static void M1 () {}
public static modifier
Void this method does not return a value after execution ends
M1 is the method name.
No formal parameters


public static int m2 (int a,boolean b) {}
public static modifier
int the return value type of this method
M2 Method Name
int A,boolean B is the formal parameter list

Note: Hard rules: If the return value type of a method is not void, then the return statement must be used in the method for the data to be returned, otherwise the compilation cannot pass. Return statement one but execution, method end.

public class methondtest{
public static void Main (string[] args) {

Invocation of the method
Methondtest.println ("Hello World");//"Hello World" is the actual parameter (argument)

Invocation of the method
METHONDTEST.M4 (13,32);


Invocation of the method
METHONDTEST.M5 ();//Because the method does not have parameters at the time of definition, it does not need to be used with parameters when calling


Invocation of the method
METHONDTEST.M6 (100,200);
Call M6 and output its value
int RETVALUES=METHONDTEST.M6 (100,200);
SYSTEM.OUT.PRINTLN ("Calculation result is" +retvalues);

Definition of a method
The form parameter list of a method determines the type of the parameter
The name of the parameter (the name of the local variable) is arbitrary as long as it is a valid identifier.
public static void println (String msg) {//string msg is a formal parameter list (parameter)
SYSTEM.OUT.PRINTLN (msg);

}

public static void M4 (int a,int b) {
System.out.println (A + "+" +b+ "=" + (a+b));
}

public static void M5 () {
System.out.println ("hehe");
}


The method has a return value, and the program must return the value using the return statement
public static int M6 (int A, int b) {
int c=a+b;
The return c;//program executes here M6 method execution ends and returns a value.
There is no additional code behind the return statement because it cannot be executed at all.
System.out.println ("ABC");//compilation fails.


}

}

}

The introduction method calculates the number of two integers and:
public class methodtest{
public static void Main (string[] args) {
The Sumint method is called in the Main method to complete the function.
Note: The static method must be called when the "class name" is used. The way to call
Methodtest.sumint (10,20);
Methodtest.sumint (20,40);
}
Define a method to complete the summation of two integers
Note that all current methods should be defined as public static.
public static void Sumint (int a, int b) {
int c=a+b;
System.out.println (A + "+" +b+ "=" +c);
}
}

Definition of the 22.java method

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.