New course! Every coder should learn Generative AI!
Try a free lesson0
The Return Type: returns the greater one. What happens if I enter 2 equal values? Why its not error?
Why if I enter two equal values the program does not give an error? class Program { public static void main(String[ ] args) { int res = max(7, 7); System.out.println(res); } static int max(int a, int b) { if(a > b) { return a; } else { return b; } } }
2 Answers
+ 4
If the if statement is false the else is executed.
if(a > b)
Or in other words.
is 7 > 7?
No, let's go to the else statement.
else{
return b;
}
Ok, b was 7.
return 7;
If they are equal it doesn't really matter which one you return anyway.
0
Great. Now I understand. Thanks