I wrote a simple one if/elsein my code that worked great. Later I added another level ifto the first one, and was confused by his behavior. Here is a very simple code to recreate the situation:
public static void main(String[] args) {
boolean a = true;
boolean b = false;
if (a)
if (b)
System.out.println("a=true, b=true");
else
System.out.println("a=false");
}
It returns " a=false", although atrue!
It turns out that it elsecontacts the nearest one if, although I did not find it documented anywhere, and eclipse does not highlight inconsistent levels of indentation as an error (although it fixes it when formatting the file).
A very, very strong argument for using braces!
Where is the binding order registered else/ if?
And as a challenge
Is there a way to make the above code what indentation you expect without adding curly braces?