简体   繁体   中英

What is the purpose of * in java.io.*

What is the purpose of the * in java.io.*?

import java.io.*;
class Trial{
     public static void main(String[]args){
         System.out.println("Hello,World!");
     }
}

The * tells the compiler to import all top-level classes in java.io on demand. The construct is called a type-import-on-demand declaration.

From JLS §7.5.2 :

A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.

\nTypeImportOnDemandDeclaration: \n    import PackageOrTypeName .  * ; \n

So, for example, since you've included that import statement, you can use a class like java.io.File without having to prefix the type name with java.io ; you can use the simple name File .

星号表示应该导入java.io包中的所有类。

A wildcard in a package name import is used to include all the classes contained in that specific package. Check official documentation .

In addition you are able to import inner static classes to be able to refer to them without a fully qualified name, eg:

import org.package.MyClass;

//MyClass.InnerClass inner; not needed
InnerClass inner;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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