In a textbook I've been looking through for a class, it is stated, a statement can be empty (the null statement). The null statement is defined as just a semicolon.

It does absolutely nothing at execution time. The null statement is rarely used.

As a moderately experienced programmer, I find it interesting that they say rarely used, is this implies that somewhere, there is a use for it.

What could possibly be the use of such a statement?


It is mostly useless, but there are a few places where it is necessary. For example:

while (DoSomething());

This executes the DoSomething() method until it returns false. Obviously this assumes the method also performs some useful side-effect. But the empty statement, i.e. the trailing semicolon, is required, otherwise the while-loop would include the next following statement instead.

(But to contradict the other answers, for(;;) {} is not an example of empty statements, since the parenthesis after a for clause does not contain statements, it just uses the semicolon to separate (optional) expressions. See the Java grammar for details.)

  • 3
    +1 - this is the only time I've ever seen it really used (as stated, empty sections of a 'for' statement are slightly different). The only change I'd make is to point out that it is far lass confusing to read this kind of use if the null statement is left on its own line, rather than immediately after the while loop, where it just looks like an ordinary statement terminator... – Jules Aug 20 '16 at 19:50

You can use it when a statement is syntactically required but not semantically required. The most common case is to omit one of the parts of a for loop, like this example, although most people find that usage odd and hard to read. Like your book says, it's rarely used.

  • That. Is a useless language feature. I would rather be forced to explicitly read duplication than have to Google "only a semicolon java". – Chris Cirefice Aug 19 '16 at 3:33
  • 2
    @ChrisCirefice On the other hand "only a semicolon: is pretty obvious what it should do considering you know the language moderately. It's not a separate rule, it's just a consequence of other rules. Plus if you learn it once you know it forever, so I would say it's not really any issue. – Maurycy Aug 19 '16 at 8:01
  • 1
    If feel like if you find yourself writing loops like that, you're doing something wrong. – Bassinator Aug 19 '16 at 13:24
  • I have a sleep-polling method that I use to check when a device comes back on line. The code pretty much looks like this: while ( sleepPoll(device, 1000, TimeUnit.MILLISECONDS) ) { ; }, all on separate lines, of course. I could have written it as while ( true ) { if ( ! sleepPoll(device, 1000, TimeUnit.MILLISECONDS) ) { break; } }, but that would be wrong. – BobDalgleish Aug 19 '16 at 14:03
  • It's not null statement what we use also to force an exit from 'void' methods? void method(){ //code here. If(condition){return ;}}. – Laiv Aug 21 '16 at 11:03

I prefer using a while loop, but a null expression lets you write this:

for (;;) { thread.run(); }

However, a null statement let's you write this:


The primary purpose of which is to waste the time of the poor maintenance programmer who comes after you and wonders why the thread isn't running. A statement must follow a while. A lone ; is a statement. So this statement block isn't actually part of the loop.

That isn't to say there aren't good uses. They're just rare. They usually look like this:


Because this way it looks more like you mean to do that (even this sometimes isn't enough).

In assembly this null statement is called a NOP. In Python it's called a pass. In java (and the other children of c) it's a semicolon as you've said, or even assert true;

I know of a few uses:


There is a difference between an empty space and putting a zero down as an answer. It clearly says, no I didn't forget to put something here. This nothing is meant to be nothing. It's explicit nothing. This space intentionally left blank. This is how we get N/A to compile.

The null pattern

Imagine a complicated and expensive light signaling machine that repeatedly picked up a colored light bulb from a conveyer belt, screwed it into a socket, powered it on, removed it, and picked up the next one. You could control what this thing signalled by the color of bulbs you put on the belt. Great, now that it's finally working flawlessly your boss wants you to create a way to control the timing of these signals. He wants to add a gap between messages. You remember with horror that many parts care about the timing of these signals and what a pain it was to get them in sync. How are you going to control the timing without this keeping you at work over the weekend? You look over at a bin of burned out bulbs and get a, well not literally, bright idea.

Sometimes you want to use polymorphism to just turn something off without breaking anything.


When you set a breakpoint you need there to be a place for the breakpoint to be. Now sure some debuggers will let you set it on the closing curly brace but if they don't it's nice that the language lets you force there be a place to put one even if that place doesn't do anything.

Not really a java thing but there is also:

Binary fiddling

Assemblies NOP instruction allows fiddling with the compiled binary executable. But modern java compilers optimize so much away I'm not sure the semicolon doesn't just go poof. But in the old days we also used these empty spots in the executable to control timing or allow us to drop in a hook later. Without the NOP there would be actual instructions there, so if you wanted to hook you'd have to copy those instructions somewhere else to make room for the hook. This works just as well in the c language from which Java gets much of its syntax. But I don't think this hook fiddling happens much in Java. We use Aspect Oriented Programming to do that.

  • 2
    This might seem pedantic, but the example with for is not null statements. Null statements (aka empty statements) may occur same place other statements (like an if) may occur, but the parentheses following for does not allow statements between the semicolons, but only an initializer and two expression, which happen to be optional. So it is a different use of semicolon than as a statment-terminator. An empty statement would be the last semicolon in for(;;); – JacquesB Aug 20 '16 at 16:13
  • You're right, this seems pedantic :P. Fine, please note my edit. – candied_orange Aug 20 '16 at 17:15

I use it because Java is designed in a way that is not congruent with human language. The same is true of C.

A statement is statement;.

An if-then-else statement is if (condition) statement; else statement;

An if-then-else statement with compound blocks is if (condition) {statements} else {statements}

This is clearly nonsense.

I accordingly write if (condition) {statements} else {statements}; which is logical and reasonable.

If the designers of Java want to treat this as if-then-else followed by a null statement, I'm fine with that. If it makes them happy, let it make them happy.

  • It's if (condition) {statements;} else {statements;}. Also, Java is pretty consistent about not needing ;s after }. Kind of like how human languages don't often stick a period on the end of a sentence that's already terminated with a question mark. (Sure, there are the !? or ?! endings, but those are just approximations of , the interrobang character.) – 8bittree Aug 19 '16 at 15:32
  • 1
    Sweet god!! o_O – Áxel Costas Pena Mar 23 '18 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.