Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java actually do something with empty statements?

Tags:

java

jvm

The official docs just say

14.6. The Empty Statement

An empty statement does nothing.

(https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.6)

Are statements like ;;;;;;;;;;; actually compiled by the Java compiler? If so, do these statements take time to be executed, like a nop? In short: does an empty statement really do "nothing"?

like image 342
ahe Avatar asked Sep 19 '26 08:09

ahe


1 Answers

The java compiler does nothing with these statements. Hence, they are ignored. The following method

public static void test(){
  ;;;;;;;;
}

just compiles to the following Bytecode:

public static void test();
descriptor: ()V
flags: ACC_PUBLIC, ACC_STATIC
Code:
  stack=0, locals=0, args_size=0
     0: return
  LineNumberTable:
    line 39: 0

Compilers just use NOPs in certain compilation strategies.

like image 195
M. Reif Avatar answered Sep 21 '26 20:09

M. Reif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!