Dabai became the thirteenth day of the Java software siege lion (java method call, the actual parameter type of the call, the method return value type is not void, and the return statement is deep)

Java language method

Instance

public class MethodTest01(){
    
    
	public static void sum(int a,int b){
    
    
		System.out.println(a+"+"+b+"="+(a+b));
		//调用doSome方法
		MethodTest.doSome();
	}
	//主方法
	public static void main(String[] args){
    
    
		//调用sum方法
		MethodTest.sum(1,2);
	}

	public static void doSome(){
    
    
		System.out.println("do some!");
	}
}

The above code shows that the method call is not necessarily in the main method. As long as the program can be executed to the location, you can call other methods.

1. When the method is called, the number of actual parameters and formal parameters must be the same, and the data types must be the same.

When the types are different, the corresponding automatic type conversion is required

public class MethodTest02(){
    
    
	//主方法
	public static void main(String[] args){
    
    
		MethodTest02.sum();//编译错误:参数数量不同
		MethodTest02.sum(true,false);//编译错误:实参和形参的类型不是对应相同的
		MethodTest02.sum(10L,20L);//编译通过
		MethodTest02.sum(10,20);//编译通过:存在自动类型转换:int-->long
		MethodTest02.sum(3.0,20);//编译错误:参数类型不是对应相同的
		MethodTest02.sum((long)3.0,20);//编译通过
	}

	public static void sum(long a,long b){
    
    
		System.out.println(a+"+"+b+"="+(a+b));

	}
}

2. Method call

1. There is the static keyword in the method modifier list. The complete calling method is: class name. method name (argument list)
2. However, sometimes "class name." can be omitted. Under what circumstances can it be omitted?

  • For methods with static keyword in the modifier list : "Class name." can be omitted.
  • Calling methods in the same class can be omitted without writing
public class MethodTest03(){
    
    

	public static void main(String[] args){
    
    
		System.out.println("main begin");
		m1();
		System.out.println("main over");
		}

	public static void m1(){
    
    
		System.out.println("m1 begin");
		m2();
		System.out.println("m2 over");
		}
		
	public static void m2(){
    
    
		System.out.println("m2 begin");
		m3();
		System.out.println("m2 over");
		}
		
	public static void m3(){
    
    
		System.out.println("m3 begin");
		System.out.println("m3 over");
	}
}

Output result:
Output result

The code in the method is executed sequentially from top to bottom

When the return value type of the method is not void

  • When the return value type is not void: the method must ensure that the "return value;" statement is executed 100% to complete the return of the value. Without this statement, the compiler will report an error.
  • When a method has a return value, when we call this method, the method returns a value. For the caller, the return value can choose to receive or not to receive.
public class MethodTest04(){
    
    

	public static void main(String[] args){
    
    
		//调用方法
		divide(10,3);//这里没有接收这个方法的返回数据
		
		//这里接收返回值
		//采用变量接收
		//变量的数据类型需要和返回值的数据类型相同,或者可以自动类型转换
		int i = divide(10,3);
	}
	
`	/*
	需求:
		请定义并实现一个方法,该方法可以计算两个int类型数据的商,
		要求将最终计算结果返回给调用者。
	*/

	//编译错误:缺少返回值
	/*public static int divide(int a, int b){
		return;
	}

	//编译错误:方法定义的时候要求返回一个int类型,此时返回布尔类型,类型不兼容
	public static int divide(int a, int b){
		return true;
	}

	//可以:但是具体方法体中编写的代码无法满足当前的需求
	public static int divide(int a, int b){
		return 1;
	}
	
	public static int divide(int a, int b){
		int c=a/b;
		return c;
	}*/
	
	public static int divide(int a, int b){
    
    
		return a/b;
	}
}

In-depth return statement

  • As long as the java statement with the return keyword is executed, the execution of the method in which it is located ends.
  • In the "same scope", no code can be written under the return statement, because these codes will never be executed, so compilation errors are reported.
public class MethodTest05(){
    
    

	public static void main(String[] args){
    
    
		int reValue=m();
		System.out,println(reValue);
	}
	
	//编译报错:缺少返回语句,以下程序编译器认为无法百分百保证“return 1;”会执行
	/*public static int m(){
		int a=10;
		if(a>3){
			return 1;
		}
	}
	*/

	public static int m(){
    
    
		int a=10;
		if(a>3){
    
    
			return 1;
			//这里不能编写代码,编译错误,因为无法执行访问的语句
			//System.out.println("Hello");
		}
		//这里的代码可以执行
		System.out.println("Hello");
		return 0;
	}

}

Use the "return;" statement in a method whose return value type is void. The "return;" statement appears in the method whose return value is void is mainly to terminate the execution of the method.

return; terminate the method directly; break just terminates the loop.
return stronger

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112756845