​面试官疯了:While(true)和For(;;)哪个性能好?

开发 前端
有人愿意用While(true)因为他更清晰的看出来这里是个无限循环。有人愿意用For(;;),因为有些IDE对于While(true)会给出警告。至于你,爱用啥用啥!

while(true)和for(;;)都是做无限循环的代码,他俩有啥区别呢?

我承认这个问题有点无聊,并且工作中也不会有人刻意的去考虑他们之间的性能差异,也不太会有人在做这个选择之间纠结。

但是,最近竟然有很多面试官开始问这个问题,所以,那就索性理性分析一波。

准备2段代码:

public class HollisTest {


    public static void main(String[] args) {
        for(;;){
            System.out.println("this is hollis testing....");
        }
    }
}
public class HollisTest {
    
    public static void main(String[] args) {
        while (true){
            System.out.println("this is hollis testing....");
        }
    }
}

关于这个问题,网上有很多讨论,说那么多没用,直接反编译,看看字节码有啥区别就行了。

分别将他们编译成class文件:

javac HollisTest.java

然后再通过javap对class文件进行反编译,然后我们就会发现,两个文件内容,一模一样!!!

Classfile /Users/hollis/workspace/chaojue/HLab/src/main/java/HollisTest.class
  Last modified 2023-6-18; size 463 bytes
  MD5 checksum 38eddb7d25748625d7c9aa377b6f66d3
  Compiled from "HollisTest.java"
public class HollisTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #6.#16         // java/lang/Object."<init>":()V
   #2 = Fieldref           #17.#18        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = String             #19            // this is hollis testing....
   #4 = Methodref          #20.#21        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #5 = Class              #22            // HollisTest
   #6 = Class              #23            // java/lang/Object
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               main
  #12 = Utf8               ([Ljava/lang/String;)V
  #13 = Utf8               StackMapTable
  #14 = Utf8               SourceFile
  #15 = Utf8               HollisTest.java
  #16 = NameAndType        #7:#8          // "<init>":()V
  #17 = Class              #24            // java/lang/System
  #18 = NameAndType        #25:#26        // out:Ljava/io/PrintStream;
  #19 = Utf8               this is hollis testing....
  #20 = Class              #27            // java/io/PrintStream
  #21 = NameAndType        #28:#29        // println:(Ljava/lang/String;)V
  #22 = Utf8               HollisTest
  #23 = Utf8               java/lang/Object
  #24 = Utf8               java/lang/System
  #25 = Utf8               out
  #26 = Utf8               Ljava/io/PrintStream;
  #27 = Utf8               java/io/PrintStream
  #28 = Utf8               println
  #29 = Utf8               (Ljava/lang/String;)V
{
  public HollisTest();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0


  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String this is hollis testing....
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: goto          0
      LineNumberTable:
        line 5: 0
      StackMapTable: number_of_entries = 1
        frame_type = 0 /* same */
}
SourceFile: "HollisTest.java"

可以看到,两个用法,其实都是通过goto来干的,所以,这两者其实是没啥区别的。用哪个都行

有人愿意用while(true)因为他更清晰的看出来这里是个无限循环。有人愿意用for(;;),因为有些IDE对于while(true)会给出警告。至于你,爱用啥用啥!

责任编辑:姜华 来源: Hollis
相关推荐

2022-12-22 14:32:37

JavaScript编程语言

2023-08-13 16:17:31

2020-08-03 07:04:54

测试面试官应用程序

2021-11-15 09:32:06

浮点面试Java

2022-03-31 16:47:30

mysqlcount面试官

2018-08-20 15:00:32

Linux深度操作系统发行版

2022-05-23 08:43:02

BigIntJavaScript内置对象

2015-08-13 10:29:12

面试面试官

2021-05-10 08:01:12

BeanFactoryFactoryBean容器

2021-03-24 10:25:24

优化VUE性能

2021-08-02 08:34:20

React性能优化

2023-02-16 08:10:40

死锁线程

2024-04-03 15:33:04

JWTSession传输信息

2024-09-19 08:42:43

2023-12-11 07:37:08

mongodb经纬度性能

2022-10-10 12:31:37

服务器性能

2023-02-17 08:10:24

2021-07-06 07:08:18

管控数据数仓

2010-08-12 16:28:35

面试官

2021-12-02 08:19:06

MVCC面试数据库
点赞
收藏

51CTO技术栈公众号