When I was writing code with an if statement, I was often angry with Eclipse saying "There is no return !!". But I don't know why. .. ..
In order to think in a more primitive form, I wrote a method that returns the larger one when two int type arguments are passed (it is one shot using the max function, but I experimented with an easy-to-understand example. Because I wanted to).
//Code 1
public class BiggerNumber {
public static void main(String[] args) {
//TODO auto-generated method stub
System.out.println(returnBiggerNumber(250,500));
}
public static int returnBiggerNumber(int num1,int num2) {
if (num1 < num2) {
return num2;
}else if (num1> num2 ) {
return num1;
}else if (num1 == num2) {
return num1;
}else { //← This!
return num1;
}
}
}
It was the last else that I couldn't understand. If you do not write this, you will get a compile error. .. ..
For this very simple method, there are only three possible branches.
Theoretically, this should cover all the possibilities. .. .. I should be able to return whatever number comes in the argument Is there any other possibility? I was worried.
When I asked a senior with a long history of java about the cause of the error, ** "The compiler cannot determine whether all conditional branches are written without exception. Therefore, even if it is a conditional branch that cannot actually be reached, it is necessary to write it formally. 』** is what they said.
"I see!"
If so, the only possibility of branching other than <,> is ==. It may not be necessary to write the conditional expression of ==. So (I) the first thing that comes to mind is this.
//Code 2
public static int returnBiggerNumber(int num1,int num2) {
if (num1 < num2) {
return num2;
}else if (num1> num2 ) {
return num1;
}else {
return num1;
}
}
Or there is this too.
//Code 3
public static int returnBiggerNumber(int num1,int num2) {
if (num1 < num2) {
return num2;
}else if (num1> num2 ) {
return num1;
}
return num1;
}
It feels a little strange, but this is also effective. Is this a matter of taste?
//Code 4
public static int returnBiggerNumber(int num1,int num2) {
if (num1 < num2) {
return num2;
}else if (num1> num2 ) {
return num1;
}else if (num1 == num2) {
}
return num1;
}
In the first place, there should be no problem because we can return for all patterns! I was convinced that it was due to ** Swift's switch statement rules **.
That is this. ** "Is there a case clause that covers all possible values of the value type given to the switch? The default clause must exist. 』** (* By the way, it is not necessary in Java, and it is not necessary in the if statement even in the same Swift)
No, ** This is a story that has nothing to do with the return value. .. .. **I had a misunderstanding.
Whether it's a Java switch statement, a swift if statement, or a switch statement, If it is a function / method that returns a return value, it is necessary to return (return) all the values that come in.
Just in case, for example
public int returnTest(int i) {
switch (i) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
//In this way, you must always write return outside the default or switch block.
//In swift, this writing method causes an error, so write it outside the switch block.
default:
return 4;
}
}
This is a common rule, but it seems that it could not be organized due to half-finished language-specific rule knowledge.
So, I was convinced for the time being, but I would appreciate it if you could comment if you have any tsukkomi or + α.
Recommended Posts