thread.sleep(9000l)和thread.sleep(9000)之间有什么区别

时间:2017-02-15 11:42:54

标签: java

Thread.sleep(9000l);Thread.sleep(9000);

之间有什么区别

它是否与线程的睡眠时间有所不同,或者它是一样的。

1 个答案:

答案 0 :(得分:5)

完全没有区别。

Thread.sleep只有一次重载:Thread.sleep(long)。第二种形式指定int参数,但它会自动扩展为long

由于9000是常量表达式,编译器会将其重写为9000L

public void one() throws InterruptedException {
  Thread.sleep(9000);
}

public void two() throws InterruptedException {
  Thread.sleep(9000L);
}

反编译为......

  public void one() throws java.lang.InterruptedException;
    Code:
       0: ldc2_w        #2                  // long 9000l
       3: invokestatic  #4                  // Method java/lang/Thread.sleep:(J)V
       6: return

  public void two() throws java.lang.InterruptedException;
    Code:
       0: ldc2_w        #2                  // long 9000l
       3: invokestatic  #4                  // Method java/lang/Thread.sleep:(J)V
       6: return

相同的字节码,所以没有区别。