在執行程式期間需要隨時匯入 Java.lang 包嗎?


否,java.lang 包是 Java 中的預設包,因此無需顯式匯入。

即,無需匯入即可訪問此包的類。

如果您觀察以下示例,此處我們並未顯式匯入 lang 包,但我們仍可以使用 java.lang.Math 類的 sqrt() 方法來計算某個數字的平方根。

示例

即時演示

public class LangTest {
   public static void main(String args[]){
      int num = 100;
      double result = Math.sqrt(num);
      System.out.println("Square root of the number: "+result);
   }
}

輸出

Square root of the number: 10.0

更新於: 2019 年 7 月 30 日

3K+ 次瀏覽

開啟您的 事業

完成課程獲得認證

開始
廣告