I didn't understand why writing the same code again after sopln(res); when we already know that the max will be 42 only?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; } }

30th Jul 2020, 11:51 AM
Mallika Gupta
5 Answers
+ 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)
30th Jul 2020, 12:01 PM
Taste
Taste - avatar
+ 1
There is no redundant code. What method sopln are you mentioning there?
30th Jul 2020, 11:54 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Yes by sopln I am talking about system.out.println
30th Jul 2020, 12:29 PM
Mallika Gupta
+ 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..
30th Jul 2020, 12:44 PM
Jayakrishna 🇮🇳
+ 1
Oooooo...got it
30th Jul 2020, 12:47 PM
Mallika Gupta