A function is an independent block of code defined in a class to implement a certain function. In Java, functions are also called methods.
The main function of the function is to improve the reusability of the code.
Functions are all run in stack memory; the running function is on the top of the stack.
Function format:
修飾符 返回值類型 函數(shù)名 ( [ 參數(shù)類型1 參數(shù)名1,參數(shù)類型2 參數(shù)名2.... ] ){ // [ ] 里面表示可選項(xiàng),即參數(shù)不是必須的 執(zhí)行語句... return 返回值; //返回值的類型必須與返回值類型一致 }
Description:
Modifier: It can be an access modifier or a function modifier (abstract, final, static, synchronized), It can also be a combination of both.
Return value type: The data type used to limit the function return value.
Parameter type: used to limit the data type passed when calling the function.
Parameter name: It is a variable used to receive the data passed when calling the method.
return: Used to receive methods and return values ??of the specified type from the function.
Return value: This value will be returned to the caller of the function.
Example:
public class method { /* * 程序入口,主函數(shù) . * * @ 方法 <==> 函數(shù),指的是同一個(gè)東西. */ public static void main(String[] args) { // 通過函數(shù)名調(diào)用 method01(); method02(9, 3); System.out.println("5+6=" + add(5, 6)); } /* * @ 函數(shù)的格式為: * * @ 訪問修飾符 返回值類型 函數(shù)名(參數(shù)類型1 參數(shù)名1,參數(shù)類型2 參數(shù)名2....){ * * @ 執(zhí)行語句 * * @ return 返回值;//返回值的類型必須與返回值類型一致 * * @ } */ /* * @ 聲明一個(gè)靜態(tài)函數(shù)method01() 無參數(shù)無返回值 */ static void method01() { System.out.println("這是method01方法,可以通過method01();調(diào)用."); // 這個(gè)return可以省略.每個(gè)函數(shù)都是以return結(jié)束,返回到函數(shù)調(diào)用處 return; } /* * 有參數(shù)無返回值 */ static void method02(int num1, int num2) { method01(); System.out.println("這是method02方法,第一個(gè)參數(shù)是" + num1 + "第二個(gè)參數(shù)是" + num2); } /* * 有返回值的返回值類型要和要返回的數(shù)據(jù)類型一致 * * @ 例如:計(jì)算兩個(gè)整數(shù)的和,結(jié)果仍然是整型,返回值類型為int.返回值類型可以說基本數(shù)據(jù)類型,也可是自定義的數(shù)據(jù)類型 */ static int add(int num1, int num2) { int sum = 0; // 聲明一個(gè)整形變量并初始化為0 sum = num1 + num2;// 將num1和num2的和賦值給sum return sum;// 將sum的值返回到調(diào)用處 } }
For more java knowledge, please pay attention to java basic tutorial.
The above is the detailed content of How to write java function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

itertools.combinations is used to generate all non-repetitive combinations (order irrelevant) that selects a specified number of elements from the iterable object. Its usage includes: 1. Select 2 element combinations from the list, such as ('A','B'), ('A','C'), etc., to avoid repeated order; 2. Take 3 character combinations of strings, such as "abc" and "abd", which are suitable for subsequence generation; 3. Find the combinations where the sum of two numbers is equal to the target value, such as 1 5=6, simplify the double loop logic; the difference between combinations and arrangement lies in whether the order is important, combinations regard AB and BA as the same, while permutations are regarded as different;

java.lang.OutOfMemoryError: Javaheapspace indicates insufficient heap memory, and needs to check the processing of large objects, memory leaks and heap settings, and locate and optimize the code through the heap dump analysis tool; 2. Metaspace errors are common in dynamic class generation or hot deployment due to excessive class metadata, and MaxMetaspaceSize should be restricted and class loading should be optimized; 3. Unabletocreatenewnativethread due to exhausting system thread resources, it is necessary to check the number of threads, use thread pools, and adjust the stack size; 4. GCoverheadlimitexceeded means that GC is frequent but has less recycling, and GC logs should be analyzed and optimized.

fixture is a function used to provide preset environment or data for tests. 1. Use the @pytest.fixture decorator to define fixture; 2. Inject fixture in parameter form in the test function; 3. Execute setup before yield, and then teardown; 4. Control scope through scope parameters, such as function, module, etc.; 5. Place the shared fixture in conftest.py to achieve cross-file sharing, thereby improving the maintainability and reusability of tests.

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear
