Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define and call the Java method

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to define and call the Java method, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to define and call the Java method. Let's take a look at it.

I. Preface

Learning Overview: learning four different types of method applications, memory diagrams when methods are called, overloading

Learning goal: proficient in the application of methods and overloading

Second, definition and call 1. Overview

Definition: a method is a grammatical structure that encapsulates a piece of code into a function to facilitate repeated calls

Classification: generally divided into parameters (with / without) and return values (yes / none)

Benefits: improved code reusability and clearer logic

two。 Format

Modifier returns the value type method name (formal parameter list)

{

/ / method body

Return return value

}

Example:

3. Call

The method must be called by the program to run, and the call format is as follows:

Method name (...)

Example:

Int sum = add (10,20); System.out.println (sum); 4. Be careful

Modifier: public static modifier is currently used temporarily

Parameter: no, but several need to be separated by commas. When calling, you need to pass in the values of variables of the corresponding type.

Return value: if the method defines a return type, the code snippet inside the method must be reflected. If the method does not need to return a result, the return value type must be declared as void (no return value)

Example 1. Example 1

Design a method (no parameter, no return value) to print the size relationship between two numbers

Coding implementation:

Public static void main (String [] args) {getRelation (); / / calling method} public static void getRelation () {int astat10; int bau20; if (a > b) {System.out.println ("an is greater than b");} else if (ab) {System.out.println (a) } else {System.out.println (b);}}

Output result:

twenty

3. Example 3

Design a method (int type with parameters and return values) to print the maximum value of two numbers

Coding implementation:

Public static void main (String [] args) {System.out.println (getMax (10 return 20)); / / call method} public static int getMax (int areint b) / / No return value with parameter {if (a > b) {return a;} else {return b;}}

Output result:

twenty

Fourth, the memory diagram of method calls

Method is not called and is stored in the bytecode file in the method area

Method call to run in stack memory

The java program compiles and generates the class bytecode file, and the method area is stored in the main method, eat, study, eat method. Enter the main function call and run the output in the stack memory.

5. Overload 1. Overview

Definition: in the same class, there are multiple methods with the same name, but the formal parameter list is different (different type or quantity), regardless of the return value.

For example, the following examples determine whether it is a method overload?

The first one is not, it has nothing to do with the return value

The second is that the number of formal parameters is different.

The third is that the definition types of formal parameters are different.

The fourth is not in the same class.

two。 Examples

Three overloaded methods are constructed to realize the sum of two int integers, two double type data and three int type data respectively.

Coding implementation:

Public static void main (String [] args) {int result=sum (10Magne20); System.out.println (result); double result1=sum (10.0,20.0); System.out.println (result1); int result2=sum (10Lyric 20,30); System.out.println (result2);} public static int sum (int aline int b) {return astatb;} public static double sum (double ascene double b) {return ahumb } public static int sum (int a _ line int bjinint c) {return a _ r _ r _ c;}

Output result:

thirty

30.0

sixty

This is the end of the article on "how to define and call Java methods". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to define and call Java method". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report