When for loop meets return

First look at the print results and return values ​​of the following methods:

public static void main(String[] args) {
		System.out.println("Return value: " + testResult());

	}
	
	public static boolean testResult() {
		for(int i=1; i<=5; i++) {
			System.out.println("-------------->开始:" + i);
			if(i == 3) {
				return true;
			}
			System.out.println("-------------->结束:" + i);
		}
		return true;
		
	}

 print result:

--------------> Start: 1

--------------> end: 1

--------------> Start: 2

--------------> end: 2

--------------> Start: 3

Return value: true, indicating that returning a value in for is equivalent to exiting the loop.

 

1) Suppose we refactor the testResult method and extract the logic in for into a separate method:

public static boolean testResult() {
		for(int i=1; i<=5; i++) {
			test1 (s);
		}
		return true;
		
	}
	
	public static  void  test1(int i) throws NullPointerException{
		System.out.println("-------------->开始:" + i);
		if(i == 3) {
			return;
		}
		System.out.println("-------------->结束:" + i);
	}

 Also in the main method. It's just that the refactored method is directly called in the for loop of the testResult method, and the result is printed:

--------------> Start: 1

--------------> end: 1

--------------> Start: 2

--------------> end: 2

--------------> Start: 3

--------------> Start: 4

--------------> end: 4

--------------> Start: 5

--------------> end: 5

Return value: true

 

This shows that the test1(i) method uses the return; statement to try to break when i=3; but the loop is still over.

 

2) Might as well give a return value to the method called in the for loop, as follows:

public static boolean testResult() {
		for(int i=1; i<=5; i++) {
			return test2(i);
		}
		return true;
		
	}

public static  boolean  test2(int i) throws NullPointerException{
		System.out.println("-------------->开始:" + i);
		if(i == 3) {
			return true;
		}
		System.out.println("-------------->结束:" + i);
		return false;
	}

 The print result is as follows:

--------------> Start: 1

--------------> end: 1

Return value: false

 

This shows that calling a method with a boolean return value in for will cause the method to break before reaching i=3 and return a boolean value.

 

3) When a boolean value needs to be returned according to the condition in the for loop. If the code in the for loop needs to be refactored into a method, it should have a return value, but this return value cannot be boolean, we might as well use String instead, and use the returned String mark in the for loop to judge whether to exit the loop~ ~

The transformation is as follows:

public static boolean testResult() {
		for(int i=1; i<=5; i++) {
			String flag =  test3(i);
			if("yes".equals(flag)) {
				return true;
			}
		}
		return true;
		
	}

public static  String  test3(int i) throws NullPointerException{
		System.out.println("-------------->开始:" + i);
		if(i == 3) {
			return "yes";
		}
		System.out.println("-------------->结束:" + i);
		return "no";
	}

 print result:

--------------> Start: 1

--------------> end: 1

--------------> Start: 2

--------------> end: 2

--------------> Start: 3

Return value: true

 

It shows that the effect when the code in the for loop is not refactored at first is achieved~  

 

The above small example is a summary of my experience when refactoring similar code and reporting errors, because in the actual code, the code in for is repeated several times, but because the code in for needs to return a boolean value according to the judgment condition. In the process of refactoring, I first changed it to test1(i), then changed it to test2(i), and finally changed it to test3(i) to achieve the effect without refactoring.

 

I hope that my colleagues must be cautious when encountering a for loop that needs to return; or return true/false;~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326458193&siteId=291194637