为什么只自动导入 java.lang 包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2073680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:16:23  来源:igfitidea点击:

why autoimport only java.lang package?

javaimport

提问by gameover

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

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

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

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

So please explain why is this not done.

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

回答by danben

A good reason not to autoimport too much is to avoid namespace clashes. If everything in java.utilwas 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.

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

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

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

回答by BlueRaja - Danny Pflughoeft

All the good IDE's willresolve your imports automatically, only prompting when there is a conflict (two packages with the same classname).

所有好的 IDE都会自动解析您的导入,仅在发生冲突时提示(两个具有相同类名的包)。

回答by Kico Lobo

Because java.lang have the core Java language classes, and java.util for example not.

因为 java.lang 有核心的 Java 语言类,例如 java.util 没有。

But some other languages, like Groovy automatically imports java.util for you :)

但是其他一些语言,例如 Groovy 会自动为您导入 java.util :)

回答by Ken

I think the idea behind java.lang is that these classes all have some connection to the language and runtime which is special, and can't be implemented on your own. Primitive wrappers, VM security and permissions and inspection, package and class loading -- all things that must be built-in to the Java system. Everything in java.util, like collections, while incredibly useful, could be implemented in pure Java. Some parts of it (time zones come to mind) have even been implemented by third-party libraries even better.

我认为 java.lang 背后的想法是这些类都与语言和运行时有某种联系,这是特殊的,不能自己实现。原始包装器、VM 安全性和权限以及检查、包和类加载——所有必须内置到 Java 系统中的东西。java.util 中的所有内容,例如集合,虽然非常有用,但都可以用纯 Java 实现。它的某些部分(想到时区)甚至已经被第三方库更好地实现了。

Or at least, that was true back in the Java 1.0 days. Today, for example, Iterator is also integral to the language, since it's automatically used by for-each loops, right? But backwards-compatibility was always a big thing with Java, so we get to live with this inconsistency forever.

或者至少,这在 Java 1.0 时代是正确的。例如,今天,Iterator 也是语言不可或缺的一部分,因为它被 for-each 循环自动使用,对吗?但是向后兼容对于 Java 来说总是一件大事,所以我们会永远忍受这种不一致。

回答by gyorgyabraham

I came across with namespace collision even with java.lang.System(our application contains a System named class). An explicit import solved my problem, but it took some minutes until I pointed out that com.mycompany.classes.Systemisn't imported automatically for identifier System by Eclipse because it already exists in java.lang.

我遇到了命名空间冲突java.lang.System(我们的应用程序包含一个名为 System 的类)。显式导入解决了我的问题,但我花了几分钟才指出它com.mycompany.classes.System不会自动导入 Eclipse 的标识符系统,因为它已经存在于 java.lang 中。

Anyways, it isn't a good idea to pollute the class scope with too much identifiers, because your code will org.classes.**look** com.application.**like** com.classes.**this**.

无论如何,用太多标识符污染类范围并不是一个好主意,因为你的代码会 org.classes.**look** com.application.**like** com.classes.**this**.

Auto-importing java.langis a good idea because it contains the very core classes and interfaces used in java.

自动导入java.lang是一个好主意,因为它包含 java 中使用的非常核心的类和接口。

回答by gyorgyabraham

@gameover, May be every java program needs the class that comes from java.lang,

@gameover,可能每个java程序都需要java.lang的类,

but the java.util class contains the class that my be need or not that is depended on programmer. So the java have the default configuration for java.lang but we need to import to java.util classes according to our program.

但是 java.util 类包含我是否需要依赖于程序员的类。所以java有java.lang的默认配置,但是我们需要根据我们的程序导入java.util类。

回答by P Sharma

java.lang package provides fundamental classes to built java programs. Object is the root of class hierarchy so it needs to be available for every programmer whether the programmer is beginner or expert.

While if we talk about other packages they are used for enlarging the programs. For example, java.util package which is only used when Collection classes are needed. While in every program Collection classes are not used while basic datatypes are essential for every java program.

To avoid unnecessary load of other classes in program other packages are not auto imported while essential package java.lang is auto imported.

java.lang 包提供了构建 java 程序的基本类。对象是类层次结构的根,因此无论程序员是初学者还是专家,每个程序员都需要使用它。

而如果我们谈论其他包,它们用于扩大程序。例如,java.util 包仅在需要 Collection 类时使用。虽然在每个程序中都没有使用集合类,而基本数据类型对于每个 Java 程序都是必不可少的。

为了避免在程序中不必要地加载其他类,其他包不会自动导入,而基本包 java.lang 是自动导入的。

回答by Gautam Prusty

There are different reason for to become java.lang package is default 1) Actually ,whatever we are declaring variable in java program.that will be stored in object,Object class is available in java.lang package.it is super class of class hierarchy,Many we have performed operation in object class method like thread programming. 2) Many time it is required class for developing program ,that class is available in java.lang package, So, it is necessary to import,when java people developed jdk, it was default package,

成为 java.lang 包是默认的有不同的原因 1) 实际上,无论我们在 java 程序中声明什么变量,都将存储在对象中,对象类在 java.lang 包中可用。它是类层次结构的超类,很多我们已经在对象类方法中进行了操作,比如线程编程。2)很多时候开发程序需要类,这个类在java.lang包中,所以需要导入,java人开发jdk的时候是默认包,

回答by user4626367

Auto importing also leads some memory issue and some times it may conflict. Eg: Date Type in java & SQL .

自动导入也会导致一些内存问题,有时可能会发生冲突。例如: java & SQL 中的日期类型。

Also whatever the base core java objects are defined in Java.lang package . Eg: any datatype & return type all are declared in java.lang package so to do some basic program also we need this package imports.

此外,无论基本核心 java 对象是在 Java.lang 包中定义的。例如:任何数据类型和返回类型都在 java.lang 包中声明,所以为了做一些基本的程序,我们也需要这个包导入。

回答by rock_athan

Without java.langpackage, development is not possible. But without java.util, java.ioor any other packages, we can still write any number of programs. That is the reason why java.langpackage is being imported by default.

没有java.lang包,开发是不可能的。但是没有java.utiljava.io或任何其他包,我们仍然可以编写任意数量的程序。这就是java.lang默认情况下导入包的原因。