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 method with return value in Java

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Java method with return value how to define and call the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that after reading this Java method with return value how to define and call the article will have a harvest, let's take a look.

Method exercise with return value

Requirements: design a method to get the larger values of two numbers, and the data comes from the parameters.

Train of thought:

1. Define a method to get the larger of the two numbers

Public static int getMax (int a _ line int b) {}

two。 Use branch statements to deal with the size of two numbers in two cases

If (a > b) {} else {}

3. Set the corresponding return value results in two cases according to the problem setting.

If (a > b) {return a;} else {return b;}

4. Call the defined method in the main () method and save it with a variable

Public static void main (String [] args) {int result = getMax (1010); system.out.println (getMax (10));}

Sample code:

Public class methodtext {public static void main (String args []) {int result = getMax (10 return 20); System.out.println (result);} public static int getMax (int a mindint b) {if (a > b) {return a;} else {return b;}} method precautions

* methods cannot nest definitions

* void means no return value. Return can be omitted, or return can be written separately with no data added.

General format of method

Format:

Public static returns the value type method name (parameter) {

Method body

Return data

}

* when defining a method, two things should be clear:

Clear return value type: it is mainly clear whether any data is returned after the method operation is completed. If not, write void;. If so, write the corresponding data type.

Clear parameters: mainly specify the type and quantity of parameters

When calling a method:

A method of type void, which can be called directly

Non-void type methods. It is recommended to use variables to receive calls.

This is the end of the article on "how to define and call methods with return values in Java". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to define and call the method of Java with return values". 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