简体   繁体   English

为什么autoimport只有java.lang包?

[英]why autoimport only java.lang package?

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. 我知道java.lang包是由我们编写的每个java程序自动导入的,因此它中的所有类都可以自动使用。

My question is why not auto import java.util and other packages too? 我的问题是为什么不自动导入java.util和其他包呢? 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. 如果java.util所有内容都是自动导入的,然后你想引用另一个名为“Map”的类,那么你必须通过它的完全限定名称来引用它。

In response to other answers in this thread, import does not actually modify the internal representation of your class files. 为了响应此线程中的其他答案, import实际上不会修改类文件的内部表示。 In fact, here is a link to the JVM spec describing the class file structure: see that imports are not stored anywhere. 实际上,这里是一个描述类文件结构的JVM规范的链接 :请参阅导入不存储在任何地方。

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

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 :)

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. 我认为java.lang背后的想法是这些类都与语言和运行时有一些特殊的连接,并且不能自己实现。 Primitive wrappers, VM security and permissions and inspection, package and class loading -- all things that must be built-in to the Java system. 原始包装器,VM安全性和权限以及检查,包和类加载 - 必须内置于Java系统的所有内容。 Everything in java.util, like collections, while incredibly useful, could be implemented in pure Java. java.util中的所有东西,如集合,虽然非常有用,但可以用纯Java实现。 Some parts of it (time zones come to mind) have even been implemented by third-party libraries even better. 它的某些部分(时区浮现在脑海中)甚至更好地由第三方库实现。

Or at least, that was true back in the Java 1.0 days. 或者至少,在Java 1.0天中这是真的。 Today, for example, Iterator is also integral to the language, since it's automatically used by for-each loops, right? 今天,例如,Iterator也是该语言的组成部分,因为它自动被for-each循环使用,对吧? But backwards-compatibility was always a big thing with Java, so we get to live with this inconsistency forever. 但是向后兼容性对于Java来说总是很重要,所以我们永远忍受这种不一致。

@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. 但是java.util类包含了我需要或不依赖于程序员的类。 So the java have the default configuration for java.lang but we need to import to java.util classes according to our program. 所以java有java.lang的默认配置,但我们需要根据我们的程序导入java.util类。

java.lang package provides fundamental classes to built java programs. java.lang包为构建的java程序提供基础类。 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. 例如,java.util包仅在需要Collection类时使用。 While in every program Collection classes are not used while basic datatypes are essential for every java program. 虽然在每个程序中都不使用集合类,而基本数据类型对于每个java程序都是必不可少的。

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时,不会自动导入其他包。

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. 成为java.lang包有不同的原因是默认1)实际上,无论我们在java程序中声明变量,还是存储在object中,Object类在java.lang包中都可用。它是类层次结构的超类,许多我们已经在对象类方法中执行操作,如线程编程。 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, 2)很多时候它是开发程序所必需的类,该类在java.lang包中可用,因此,有必要导入,当java人开发jdk时,它是默认包,

I came across with namespace collision even with java.lang.System (our application contains a System named class). 即使使用java.lang.System (我们的应用程序包含一个名为class的System),我也遇到了名称空间冲突。 An explicit import solved my problem, but it took some minutes until I pointed out that com.mycompany.classes.System isn't imported automatically for identifier System by Eclipse because it already exists in java.lang. 显式导入解决了我的问题,但是我花了几分钟才指出com.mycompany.classes.System没有自动导入标识符System by 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.lang is a good idea because it contains the very core classes and interfaces used in java. 自动导入java.lang是一个好主意,因为它包含java中使用的非常核心的类和接口。

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 . 无论Java.lang包中定义了什么基本核心java对象。 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.lang包中声明,所以要做一些基本程序我们也需要这个包导入。

Without java.lang package, development is not possible. 没有java.lang包,开发是不可能的。 But without java.util , java.io or any other packages, we can still write any number of programs. 但是没有java.utiljava.io或任何其他包,我们仍然可以编写任意数量的程序。 That is the reason why java.lang package is being imported by default. 这就是为什么默认情况下导入java.lang包的原因。

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

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