Home >Java >Javagetting Started >what is java method
Java's method is a block of code that can be called repeatedly.
##Method declaration: (Recommended learning: java course )
public static 方法返回值 方法名([参数类型 变量……]){ 方法代码体; return 返回值; }
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); } }
Run result:
2 3 10
The above is the detailed content of what is java method. For more information, please follow other related articles on the PHP Chinese website!