I have an idea for how the ? operator works: it is shorthand for if else statements. though I'm confused as to how it can be put as a return value of a method. At first, I thought the operator was syntactically equivalent (i.e. in any context, you can interchange the two and the program will yield the same output.) I try to test this by putting an if else statement as a return value, but I get an error.
perhaps I'm not fully understanding the nature of return statements? personally, I learn how to code best when I have at least a very good idea of what the code is saying rather than "oh, it passed all the tests! I don't know how though!"
any thoughts?
package com.codegym.task.task02.task0217;
/*
Minimum of four numbers
*/
public class Solution {
public static int min(int a, int b, int c, int d) {
int m = 0;
min(b, c) ? m = b : m = c;
min(c, d) ? m = c : m = d;
return m;
public static int min(int a, int b) {
return (a <= b) ? a : b;
}
public static void main(String[] args) throws Exception {
System.out.println(min(-20, -10));
System.out.println(min(-20, -10, -30, -40));
System.out.println(min(-20, -10, -30, 40));
System.out.println(min(-40, -10, -30, 40));
}
}