Get the App
SLTechnology News&Howtos  ›  Development  › 

How to define and use the Java method

Shulou Source: shulou.com Published: 2022-06-01 05:52:12 10月03日 Update

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

Tags: Method function two example name number number sum code memory time knowledge factorial input maximum some fairy flower even number content function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi macOS MariaDB Xiaomi