简体   繁体   English

为什么我在使用 Runtime 时不需要导入 java.lang?

[英]Why do not I need to import java.lang when using Runtime?

In the piece of code below, Why do I need to import java.awt.Robot but I can use Runtime without importing java.lang.Runtime ?在下面的一段代码中,为什么我需要导入java.awt.Robot但我可以在不导入java.lang.Runtime情况下使用Runtime

package classes;

import java.awt.Robot;

public class RuntimeExecution {
    public static void main(String[] args) {
        try {
            Runtime.getRuntime().exec("notepad");
            Robot robot = new Robot();
            robot.keyPress(65);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Because 因为

Every compilation unit implicitly imports every public type name declared in the predefined package java.lang , as if the declaration import java.lang.*;每个编译单元都隐式导入在预定义包java.lang声明的每个公共类型名称,就好像声明import java.lang.*; appeared at the beginning of each compilation unit immediately after any package statement.在任何 package 语句之后立即出现在每个编译单元的开头。 As a result, the names of all those types are available as simple names in every compilation unit.因此,所有这些类型的名称在每个编译单元中都可以作为简单名称使用。

where a compilation unit is basically your .java source file.其中编译单元基本上是您的.java源文件。

For convenience, the Java compiler automatically imports two entire packages for each source file:为方便起见,Java 编译器会自动为每个源文件导入两个完整的包:

  1. The java.lang package and java.lang 包和
  2. The current package (the package for the current file).当前包(当前文件的包)。

Please ref. 请参考。 oracle.com for more info (http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html) oracle.com 了解更多信息 (http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM