Today, after half an hour of trying to find a bug, I found that it is possible to placed a semicolon after an if statement rather of code, choose this:

if(a == b);// execute stuffWhich basically way that the stuff will certainly be excellent whether a equates to b or not, and also the if statement has actually no allude whatsoever. Why doesn"t Java provide me one error? Is there any type of situation in i beg your pardon this would certainly be useful?


*

*

Why does it happen?

Java Language Specification says that:

The north Statement

An north statement go nothing.

You are watching: If you place a semicolon after the statement

EmptyStatement: ;Execution the an north statement always completes normally

It essentially means that you want to execute north statement if a==b

if(a == b);

What should you do:

There room two key solutions come this problem:

You deserve to avoid problems with north statement by utilizing code formatterand bordering stuff inside if through and also . By doing thisYour north statement will certainly be much an ext readable.

if(a == b) ;You can likewise check tools provided for revolution code evaluation such as:

*

They deserve to instantly highlight difficulties such together this one.

I would recommend to integrate both solutions.


re-publishing
boost this price
monitor
edited Jun 20 "20 at 9:12
*

CommunityBot
111 silver badge
answered january 1 "13 at 17:25
*

Marcin SzymczakMarcin Szymczak
9,97544 yellow badges4949 silver badges6161 bronze title
3
include a comment |
29
Is there any type of situation in i beg your pardon this would certainly be useful?

Useful? together in "makes your password cleaner, clearer, faster, more maintainable"? no at all. This is most likely poor, confusing code.

But it"s no necessarily benign. Together a statement deserve to perform plot and/or alter state as result of methods which reason side effects, and also optionally advice those methods as result of short-circuiting that operators.

if( a() && b() );Here, a() or b() might do something, and b() will just execute if a() is true.

As come why, ns think the prize is just that it would certainly be worse come deviate indigenous defined, expected actions (e.g. Statements favor while(reader.read());) than the alternate of developers writing poor code.

Writing poor code is constantly possible. And just to reiterate, this would certainly be bad code in nearly any case.


share
boost this answer
follow
edited jan 2 "13 in ~ 16:07
answered january 1 "13 in ~ 17:25
*

Tim MedoraTim Medora
51.9k1111 yellow badges115115 silver badges155155 bronze badges
0
include a comment |
19
A feasible use case:

if (a==b);else // perform somethingNot good, but possible.

Still, I carry out think that the Java specification need to disallow an north if.


share
boost this answer
monitor
edited Feb 28 "16 in ~ 2:45
Peter Mortensen
29k2121 gold badges9797 silver badges124124 bronze title
answered jan 1 "13 in ~ 17:30
gefeigefei
17.5k66 yellow badges4747 silver- badges6464 bronze badges
4
add a comment |
12
If you"re using Eclipse, you can make it warn you around those statements:

*
Compiler->Errors/Warnings">


re-publishing
improve this answer
follow
answered may 7 "13 at 22:09
nullptrnullptr
2,23411 gold badge1313 silver badges2222 bronze title
include a comment |
8
If you use an if statement, the very first statement ~ the if will be enforcement if the condition is true. If you have a block after the if (with curly braces), that counts for that totality block. If there is no block the counts for just one statement. A solitary semicolon is an empty statement. You could likewise write the password from you instance like this:

if(a==b) ;
re-superstructure
boost this price
follow
answered january 1 "13 in ~ 17:27
André StannekAndré Stannek
7,5033232 silver- badges4848 bronze badges
0
add a comment |
8
It is one old leftover indigenous the days when there was an ext syntactic street to distinguish expressions native statements.

Basically, the comma was offered as the list item separator, therefore the semicolon was supplied as the "list of statements" separator. The downside is in the handling of null item in lists, and null declaration in blocks.

In a perform of items, Java provides the clear keyword null, yet a "null statement" is simply an north line. Allowing the existence of an empty line is a holdover from heritage inherited from C.

Why execute it? particularly with one if statement when you know that no statements room being executed: due to the fact that some if statements have side effects:

int c; if ((c = in.read()) != -1);Yes, that is no the finest example, however basically the says read a byte from the stream and also do nothing. Can be advantageous in some corner cases, but even if this example isn"t the best, that illustrates the intent. We desire to feeling the side-effects that the expression there is no accidentally executing any kind of statements.


re-superstructure
improve this price
monitor
answered january 1 "13 in ~ 22:13
Edwin BuckEdwin Buck
66k77 gold badges9393 silver badges130130 bronze title
add a comment |
5
I can"t think of one occasion where it is useful. It have the right to be helpful for loops like

while(do something);or

for(init; perform something; other else);If you use your code formatting in your concept regularly these kind of bugs end up being obvious. Some IDEs to mark this together a probable an insect as well.


share
enhance this price
monitor
answered january 1 "13 at 17:25
Peter LawreyPeter Lawrey
505k7373 gold badges716716 silver badges10911091 bronze title
0
include a comment |
4
I"d agree v you there"s no beneficial purpose come this for a human. I doubt it"s there since it simplifies the language definition; it way that the thing that comes after one if is e very same as the point that comes after a while, for instance.


share
enhance this price
monitor
answered january 1 "13 at 17:27
yshavityshavit
40.4k77 yellow badges7777 silver badges115115 bronze title
include a comment |
3
Why? It"s due to the fact that its easier for compiler writers. Girlfriend don"t have to make a special instance to inspect for semicolons ~ if(cond) and also has an added usage of permitting

if (cond && maybeFunc()) ;// Code right here I desire to ignoreEven despite it"s in reality a damaging idea to permit this. It"s just simpler to permit and then to add a situation to check this.


share
enhance this prize
follow
edited Feb 28 "16 at 2:51
Peter Mortensen
29k2121 gold badges9797 silver badges124124 bronze title
answered jan 1 "13 in ~ 23:57
user34537user34537
1
include a comment |
2
Java enables an north block any kind of place a statement block is allowed. Ns am sure making this a general preeminence for all blocks simplifies the compiler.

I agree that this is primarily the reason of bugs that room spectacularly hard to find. I always use braces roughly blocks, also when over there is a solitary statement, yet Java allows you to do a block with braces at any type of point, so using braces have the right to not conserve you indigenous this fate. Because that example, I when wasted 4 hrs trying uncover something like this:

while (condition); statement; statement;The semicolon in ~ the end of the very first line to be a typo, accidentally make the explain block because that the if loop empty. Because the syntax is valid the regime compiled and ran fine, simply not the method I wanted it to. It was really difficult to find.

See more: True Or False Lipids Are A Poor Source Of Stored Energy., Carbohydrates, Proteins, And Fats

I have the right to think of one instance where that is very nice the you are allowed to have actually empty blocks, and this is something like this:

if (condition1) do_action_1();else if (condition2) //nothing really to do in this caseelse if (condition3) do_action2();else do_action3();In the above example, you want to be able to separate out miscellaneous conditions. Remember, those problems might it is in overlapping, so the is no always possible to rearrange the order. If among the conditions really does not require anything done, then it is nice that Java allows you to have actually an north block. Otherwise, the language would need some kind of a "noop" an approach to use once you really do not want anything done.

I personally would favor the clearly "noop" explain -- yet that is not how Java is defined.