Introduction to java | Definition and use of methods

Introduction to java (6) | Definition and use of methods Start from scratch!!! In the previous period, we explained a...
1. Method
2. Method overload

Introduction to java (6) | Definition and use of methods
Start from scratch!!!

In the previous period, we explained and practiced the branch structure of java, such as if, if (judgment statement..)else, else if, and switch case, how do you understand them?
.

This is an introduction to the method method, which is also called a function. Its basic format is:
The name of the method's modifier method's return value method (parameter list)

1. Method

1.1 Overview

Method s in Java may also be called Function s in other languages.For some complex code logic, if you want to reuse the code and use it "anytime, anywhere", you can put it in a brace "{}" and give it a name.When using code, simply find the name call.

Form 1.2

First, learn about two concepts about methods:
1. Parameter: refers to the data that enters the method, with which the method can execute logic.
2. Return value: refers to the data that comes out of the method, that is, the final result data after the method is executed
The basic format of the methods currently defined:

The name of the method's modifier method's return value method (parameter list) 1.3 Exercise 1: Method Calls

package cn.qile.method; //Use of test methods public class Test1_Method { public static void main(String[] args) { System.out.println(1); method();//1. Calls between methods System.out.println(2); } //Create method() //The name of the method's modifier method's return value method (parameter list) public static void method() { System.out.println(3); System.out.println(4); System.out.println(5); } }

Run result:

1 3 4 5 2 1.4 Exercise 2: Method Passage
package cn.qile.method; //Use of test methods public class Test1_Method { public static void main(String[] args) { // System.out.println(1); // method();//1, call between methods // System.out.println(2); method2(10);//2. Pass-through between methods, parameter passed when calling--argument method3("10",5);//2.1. Pass multiple parameters separated by commas } //TODO creates method3("jack",5) parameter type parameter name - - parameter //Where the parameter type must be the same as the parameter type when invoked public static void method3(String n,int a) { //+is a concatenation of strings, and between two numbers+is used for addition //Use + is a stitched string whenever there is a string type System.out.println(n+a);//105 } //TODO Create method2(10) //The name of the method's modifier method's return value method (parameter type parameter name) public static void method2(int number) { System.out.println(number); } //Create method() //The name of the method's modifier method's return value method (parameter list) public static void method() { System.out.println(3); System.out.println(4); System.out.println(5); } }

Run result:

10 105 1.5 Exercise 3: Return values
package cn.qile.method; //Use of test methods public class Test1_Method { public static void main(String[] args) { //// System.out.println(1); //// method();//1, call between methods //// System.out.println(2); // // method2(10);//2, passing between methods, parameter passed when calling - argument // Method3 ("10, 5);//2.1, pass multiple parameters, comma separated int result = method4(10,5);//3. Return Value of Method System.out.println(result); } //TODO method4(10,5) public static int method4(int a,int b) { //Return keyword, you can return the result of the operation, call the location return a+b; } //TODO creates method3("jack",5) parameter type parameter name - - parameter //Where the parameter type must be the same as the parameter type when invoked public static void method3(String n,int a) { //+is a concatenation of strings, and between two numbers+is used for addition //Use + is a stitched string whenever there is a string type System.out.println(n+a);//105 } //TODO Create method2(10) //The name of the method's modifier method's return value method (parameter type parameter name) public static void method2(int number) { System.out.println(number); } //Create method() //The name of the method's modifier method's return value method (parameter list) public static void method() { System.out.println(3); System.out.println(4); System.out.println(5); } }

Run result: 15

2. Method overload

2.1 Concepts

Method overload refers to defining multiple methods with the same name in a class.
However, each method is required to have a different parameter list (that is, the number and type of parameters are different).
When a program calls a method, it can decide which method to use by passing different numbers and types of parameters to them.

2.2 Exercise: Sum of Numbers
package cn.qile.method; //Test method overload //Meaning of overloading: If more than one method with the same name is provided, the difference is the parameter list. //The purpose is to facilitate external calls and reflect the flexibility of the program public class Test2_Overload { public static void main(String[] args) { //Overload: Method names are the same + parameter lists are different (number or type of parameters are different) int sum = method(10,5); System.out.println(sum); String sum2 = method("jack",10,5); System.out.println(sum2); String sum3 = method(10,5,"jack"); System.out.println(sum3); } //Create method(10,5,"jack") public static String method(int a,int b,String c) { return a+b+c;//15jack } //Create method("jack",10,5) public static String method(String n,int o,int p){ return n+o+p;//jack105 } //Create method(10,5) -- where 10 and 5 are arguments and a and b are formal parameters public static int method(int a,int b) { return a+b; } }

Run result:

15 jack105 15jack

Next issue: Getting started with java (7) | Loop structure

Focus on [Kele is not a farmer], reply [Get started with java] and watch the next issue ahead of time!!

Happy waiting for you

16 June 2020, 22:25 | Views: 5067

Add new comment

For adding a comment, please log in
or create account

0 comments