I really don't understand this kind of "operations" or what are they called :
System.out.println((1<2) ?5: (3<4) + " ");
Is the same with this code ?
if(1<2)
return 5;
else if (3<4)
But after ':' it says Dead code . Why is that ?
Compiler evaluates constant expressions at compile time. Because of that, the expression 1<2 is fully equivalent to the expression true, making the initial part of your conditional expression look like this:
System.out.println((true) ? 5 : (3<4) + " ");
// ^^^^^^^^^^ ^^^^^
// Important code |
// |
// Useless code -----------+
The compiler does not stop at evaluating 1<2, it goes on to evaluating the rest of the expression, which produces 5.
Your code is weird, I really do not understand what are you trying to achieve. I think it is wrong, it should be like this:
return 1 < 2 ? 5 : 3 < 4 ? 1 : 2;
You can rewrite it into if-else form:
if (1 < 2)
return 5;
if (3 < 4)
return 1;
return 2;
Since return causes the program to jump out of function, else is not needed in this particular example.
Dead Codeinforms you about code that can't ever be reached. This is the case since 1 will always be smaller than 2. Please read alvinalexander.com/java/edu/pj/pj010018 to learn more about theternary operator(that is what this construct is called)