How to define and use the Java method
This article mainly introduces Java method how to define and use the relevant knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe everyone read this Java method how to define and use the article will have some gains, let's take a look at it.
1.1 What is a method?
A method is just a snippet of code. Similar to "function" in C language. Method can be understood as a function that can be reused.
1.2 method definition syntax
For now, writing any method is written as:
pubic static return value return name (list of formal parameters){
Function body/method body
}
Code example: Find the sum of 1-n
/** * Ask for the sum of 1-n. * Function names must be small humps * @param n Enter a number */ public static int sumAdd(int n){ int sum = 0; for(int i = 1;i