A loop with an empty body in Java

前端 未结 4 1777
时光说笑
时光说笑 2021-01-18 06:53

During bug fixing in very old project I\'ve faced with strange method, it looks like this:

   void waiter() {
        for (int i = 0; i < 20000; i++) ;
           


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 07:34

    It will be optimized after few runs by JIT. The JVM , at the first run, needs to check if the value if i that is being incremented is not being used anywhere.

    Check this article as well :

    Java: how much time does an empty loop use?

提交回复
热议问题