What will be the result of the following code execution?

public class Main {
    public static void main(String[] args) {
        int i = 0;
        int j = 1;
        System.out.println(i += (j < i) ? (2) : (3)); //1
    }
}
Explanation
Conditional statement (condition) ? (value1) : (value2) is used in the provided example. Condition is placed before the question mark, and is followed by two returned results separated by : sign. If condition results in true then the first value will be returned, othervise - second value will be returned.

I think the explanation would do well with an addendum about why println accepts something like (i = 2), something about unary/binary operators or someting, I dunno. It's a really strange thing

2023 Feb 17, 10:28:55 AM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes