Home >Java >javaTutorial >How to define and call Java methods

How to define and call Java methods

王林
王林forward
2023-05-01 08:13:131161browse

1. Preface

  • Learning overview: Learn four different types of method applications, the memory map when the method is called, and overloading

  • Learning goals: Proficient in the application and overloading of methods

2. Definition and calling

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), return value (with /None)

  • Benefits: Improve code reusability and clearer logic

2. Format

Modifier return value type method name (formal parameter list)
{
//Method body
return return value;
}

Example:

How to define and call Java methods

3. Calling the

method must be called through a program to run. The calling format is as follows:

Method name (…);

Example:

int sum = add(10, 20);
System.out.println(sum);

4. Note the

  • modifier: currently use public static modification

  • Formal parameters: There can be none, but multiple ones need to be separated by commas. When calling, you need to pass in the variable value of the corresponding type

  • Return value: If the method defines a return type, the value inside the method The code snippet 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)

3. Example

1. Example 1

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

Encoding implementation:

public static void main(String[] args) 
{
	getRelation();//调用方法
}
public static void getRelation()
{
	int a=10;
	int b=20;
	if(a>b)
	{
		System.out.println("a大于b");
	}
	else if(a<b)
	{
		System.out.println("a小于b");
	}
	else
	{
		System.out.println("a等于b");
	}
}

Output result:

a is less than b

2. Example 2

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

Encoding implementation:

public static void main(String[] args) 
{
	getMax(10,20);//调用方法
}
public static void getMax(int a,int b)//带参无返回值
{
	if(a>b)
	{
		System.out.println(a);
	}
	else
	{
		System.out.println(b);
	}
}

Output result:

20

3. Example 3

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

Encoding implementation:

public static void main(String[] args) 
{
	System.out.println(getMax(10,20));//调用方法
}
public static int getMax(int a,int b)//带参无返回值
{
	if(a>b)
	{
		return a;
	}
	else
	{
		return b;
	}
}

Output result:

20

4. Method The called memory map

How to define and call Java methods

  • method is not called, and is stored in the bytecode file in the method area

  • Method call, enter the stack memory and run

  • java program is compiled to generate a class bytecode file, and the main method, eat, study, and eat methods are stored in the method area. Enter the main function call and run the result in the stack memory.

5. Overloading

1. Overview

Definition: In the same class, there are multiple methods with the same name but different formal parameter lists. (Different types or different numbers), regardless of the return value

For example, in the following examples, determine whether it is method overloading?

How to define and call Java methods

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

  • The second one is, the amount of formal parameters Different

  • The third one is that the formal parameter definition types are different

  • The fourth one is not, they are not in the same class

2. Example

Construct three overloaded methods to implement the sum of two int types, the sum of two double types, and the sum of three int types. Sum of data addition

Encoding implementation:

public static void main(String[] args)
{
	int result=sum(10,20);
	System.out.println(result);
	double result1=sum(10.0, 20.0);
	System.out.println(result1);
	int result2=sum(10, 20,30);
	System.out.println(result2);
}
public static int sum(int a,int b)
{
	return a+b;
}
public static double sum (double a,double b)
{
	return a+b;
}
public static int sum(int a,int b,int c)
{
	return a+b+c;
}

Output result:

30
30.0
60

The above is the detailed content of How to define and call Java methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete