Home > Java > Javagetting Started > what is java method

what is java method

(*-*)浩
Release: 2019-11-09 11:58:38
Original
3685 people have browsed it

Java's method is a block of code that can be called repeatedly.

what is java method

##Method declaration: (Recommended learning: java course )

   public static 方法返回值 方法名([参数类型 变量……]){
         方法代码体;
         return 返回值;
}
Copy after login

The definition of a method includes two parts: method header and method body.

The method header can consist of the method's type, name, parentheses after the name, and a list of parameters.

The method body consists of a pair of brackets and the content between the brackets. The content includes java statements and variable declarations (referring to local variables).

When the method is declared with void, then the method has no return value; (return can be used to end the call, often used in conjunction with the if statement)

If the method has a return value, return Values ​​can be primitive types and reference types.

public class Test6 {
    public static void myPrint(int x){
        if(x==1){
            return;//若果执行该条语句,则后面的语句不执行,方法调用完毕。
        }else{
            System.out.println(x);
        }
    }
    public static void main(String[] args){
    myPrint(2);
    myPrint(3);
    myPrint(1);
    myPrint(10);
    }
}
Copy after login

Run result:

2 3 10
Copy after login

The above is the detailed content of what is java method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template