+ 1
I didn't understand why writing the same code again after sopln(res); when we already know that the max will be 42 only??
public static void main(String[ ] args) { int res = max(7, 42); System.out.println(res); //42 } static int max(int a, int b) { if(a > b) { return a; } else { return b; } }
6 odpowiedzi
+ 3
most of the time you dont know the value that being used.
your example directly use a value, which is yes it doesnt make sense. but it show how a method works, and thats the point for that particular example.
if we're expecting user input, there's no guarantee that user input will always be the same, and that is where we need to use the method (in this case check which of 2 values are bigger)
+ 1
There is no redundant code. What method sopln are you mentioning there?
+ 1
Yes by sopln I am talking about system.out.println
+ 1
Mallika Gupta
Max is just your (user) defined name given to your function.. There is no predefined function of max() in java default package java.lang. So compiler can't no what is max(7,42) until you provide defination. So must provide it..
But Math class has max function.. So if you use Math.max(7,42); then no need that function defination again...
Hope it make sence..
+ 1
Oooooo...got it