使用Runtime时为什么不需要导入java.lang? [英] Why do not I need to import java.lang when using Runtime?

查看:129
本文介绍了使用Runtime时为什么不需要导入java.lang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,为什么我需要导入 java.awt.Robot 但我可以使用运行时不导入 java.lang.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?

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());
        }
    }
}


推荐答案

为方便起见,Java编译器会自动为每个源文件导入两个完整的包:

For convenience, the Java compiler automatically imports two entire packages for each source file:


  1. java.lang包和

  2. 当前包(当前文件的包
    )。

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

这篇关于使用Runtime时为什么不需要导入java.lang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆