Well, there is of course no difference between them as such at runtime.
But you should certainly use the 2nd way for the sake of maintainence of your code.
Why I'm saying this is, suppose in future, you need to add some more lines to your if-else
block to expand them. Then if you have the first way incorporated in your old code, then you would have to add the braces
before adding some new code. Which you won't need to do in 2nd case.
So, it is far easier to add code to the 2nd way in future, than to the 1st one.
Also, if you are using the first way, you are intended to do typing errors, such as semi-colon
after your if
, like this: -
if (a > 0);
System.out.println("Hello");
So, you can see that your Hello
will always get printed. And these errors you can easily remove if you have curly braces
attached to your if
.