为什么只自动导入 java.lang 包? [英] why autoimport only java.lang package?

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

问题描述

我知道包 java.lang 是由我们编写的每个 Java 程序自动导入的,因此我们可以自动使用其中的所有类.

我的问题是为什么不自动导入 java.util 和其他包?这肯定会节省一些打字:)

所以请解释为什么没有这样做.

解决方案

不要过度自动导入的一个很好的理由是避免命名空间冲突.如果 java.util 中的所有内容都是自动导入的,然后您想引用名为Map"的不同类,例如,您必须通过其完全限定名称来引用它.>

响应此线程中的其他答案,import 实际上并未修改类文件的内部表示.事实上,这里有一个链接 到描述类文件结构的 JVM 规范:看到导入没有存储在任何地方.

I know that the package java.lang is auto-imported by every java program we write, hence all the classes in it are automatically available to us.

My question is why not auto import java.util and other packages too? That sure will save some typing :)

So please explain why is this not done.

解决方案

A good reason not to autoimport too much is to avoid namespace clashes. If everything in java.util was imported automatically and then you wanted to refer to a different class named 'Map', for example, you would have to refer to it by its fully-qualified name.

In response to other answers in this thread, import does not actually modify the internal representation of your class files. In fact, here is a link to the JVM spec describing the class file structure: see that imports are not stored anywhere.

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

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