Java 导入语句语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20777260/
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
Java import statement syntax
提问by user3081519
This is a simple question, but I am really bugged by it. I was trying to find a duplicate, and googled it, but I was more surprised when I couldn't find a satisfying answer.
这是一个简单的问题,但我真的被它困扰了。我试图找到一个重复的,然后用谷歌搜索它,但当我找不到令人满意的答案时,我感到更加惊讶。
import java.util.Scanner;
In this statement .Scanner
is the class,
在这个语句中.Scanner
是类,
.util
is the name of the package
.util
是包的名称
What is java
or javax
or whatever would stand before the first period in general?
什么是java
或javax
或一般第一期之前任何会站?
UPDATE:
更新:
I also found this picture:
我还找到了这张图:
http://www.javatpoint.com/package
http://www.javatpoint.com/package
Is it true?
这是真的吗?
采纳答案by Radiodef
Per the JLS 7.1:
根据JLS 7.1:
The members of a package are its subpackages and all the top level class types (§7.6, §8) and top level interface types (§9) declared in all the compilation units (§7.3) of the package.
For example, in the Java SE platform API:
The package java has subpackages awt, applet, io, lang, net, and util, but no compilation units.
The package java.awt has a subpackage named image, as well as a number of compilation units containing declarations of class and interface types.
If the fully qualified name (§6.7) of a package is P, and Q is a subpackage of P, then P.Q is the fully qualified name of the subpackage, and furthermore denotes a package.
包的成员是它的子包以及包的所有编译单元(第 7.3 节)中声明的所有顶级类类型(第 7.6 节、第 8 节)和顶级接口类型(第 9 节)。
例如,在 Java SE 平台 API 中:
包 java 有子包 awt、applet、io、lang、net 和 util,但没有编译单元。
包 java.awt 有一个名为 image 的子包,以及许多包含类和接口类型声明的编译单元。
如果一个包的全限定名(第 6.7 节)是 P,而 Q 是 P 的一个子包,那么 PQ 就是这个子包的全限定名,而且还表示一个包。
So you can glean from that:
所以你可以从中收集:
java
is a package with no classes, only subpackages.util
is a subpackage ofjava
whose fully qualified name isjava.util
.util
does not denote a package,java.util
does.
java
是一个没有类,只有子包的包。util
是java
完全限定名称为的子包java.util
。util
不表示包,java.util
是。
"I also found this picture: ... Is it true?"
“我还发现了这张图:……是真的吗?”
Yes, util
is a subpackage of java
. However, util
is not a package. java.util
is a package.
是的,util
是java
. 然而,util
不是一个包。java.util
是一个包。
You can think ofpackages as a directory structure, if you wish, where each subpackage is a folder inside its outer package. So there would be a "folder" java and, inside that, another "folder" util. A package is denoted by its fully qualified name ("full path") so java
is a package and java/util
is a package. /util
is not a package. But packages represented by a directory structure is not a spec. It is only a common implementation. It is up to the host system to decide how packages are stored (JLS 7.2).
如果您愿意,您可以将包视为一个目录结构,其中每个子包都是其外部包内的一个文件夹。所以会有一个“文件夹”java,在里面还有另一个“文件夹”util。包由其完全限定名称(“完整路径”)表示,因此java
包java/util
也是包。/util
不是一个包。但是由目录结构表示的包不是规范。这只是一个常见的实现。由主机系统决定如何存储包(JLS 7.2)。
回答by Darkhogg
Classes in Java are identified by a fully qualified nameconsisting in a concatenation of the package of the class and the name of the class (and any outer classes, if any). In general, in an import statement like:
Java 中的类由完全限定的名称标识,该名称由类的包和类的名称(以及任何外部类,如果有)的串联组成。通常,在导入语句中,例如:
import foo.bar.baz.MyClass;
everything except the last dot-separated field is the package name (foo.bar.baz
) and the last field is the class name (MyClass
). In your example, java.util
is the package name and Scanner
is the class name.
除了最后一个点分隔字段之外的所有内容都是包名 ( foo.bar.baz
),最后一个字段是类名 ( MyClass
)。在您的示例中,java.util
是包名,Scanner
是类名。
The process is actually a bit more complicated, as inner/nested classes and interfaces may be involved, but you get the idea.
这个过程实际上有点复杂,因为可能涉及内部/嵌套类和接口,但你明白了。
回答by Tanmay Patil
1) java is a package. (also represents a folder in file system).
It is directly in the classpath, so it is referenced by your program as 'java'. (subfolder in java folder)
2) util is a package inside java package (hence referenced as 'java.util').
3) Scanner is a class inside util package (hence 'java.util.Scanner')
1)java是一个包。(也代表文件系统中的一个文件夹)。
它直接位于类路径中,因此您的程序将其引用为“java”。(java 文件夹中的子文件夹)
2)util 是 java 包中的一个包(因此称为“java.util”)。
3) Scanner 是 util 包中的一个类(因此是“java.util.Scanner”)
You can have as many nested packages as you want like 'mypackage1.mypackage2.mypackage3. ...' and so on, as long as mypackage1 is in the classpath.
您可以拥有任意数量的嵌套包,例如“mypackage1.mypackage2.mypackage3”。...' 等等,只要 mypackage1 在类路径中。
Hope this helps
希望这可以帮助
回答by Tim B
import java.util.Scanner
says.
import java.util.Scanner
说。
- Look in the package java.
- Within that look in the package util.
- Within that find the class Scanner.
- Now whenever we use the name of a class/etc within this java file (for example
Scanner s = new Scanner()
) then the class found by the import will be used.
- 查看包java。
- 在包中的那个外观中。
- 在其中找到类 Scanner。
- 现在,每当我们在这个 java 文件(例如
Scanner s = new Scanner()
)中使用类/等的名称时,将使用导入找到的类。
Alternatively you could not do the import and do java.util.Scanner s = new java.util.Scanner()
but you can see how that would quickly become unwieldy, especially if you use it in a lot of places within your file. Imports are just a handy way to reduce repeatedly specifying which version of the Scanner
class you mean when you refer to Scanner
.
或者,您无法进行导入并执行,java.util.Scanner s = new java.util.Scanner()
但您可以看到这将如何迅速变得笨拙,特别是如果您在文件中的很多地方使用它。导入只是一种方便的方法,可以减少Scanner
在引用时重复指定您指的类的哪个版本Scanner
。
回答by Bohemian
A few points:
几点:
- the package name is
java.util
, notutil
. "java" is just part of the package name. - package names are any series of valid java identifiers separated by dots,
AbC123.XYZ.foo
is a valid package name - package may be omitted. If absent, the class is in the root directory of the project (I once worked on a project in production that had no packages! Everything was in one directory... Yikes!)
- by convention, packages starting with
java
are part of the JDK (plus extensions). There is nothing in the language that specifies this or enforces it
- 包名是
java.util
,不是util
。“java”只是包名的一部分。 - 包名是由点分隔的任何一系列有效的java标识符,
AbC123.XYZ.foo
是一个有效的包名 - 包可以省略。如果不存在,该类位于项目的根目录中(我曾经在一个没有包的生产项目中工作!一切都在一个目录中......哎呀!)
- 按照惯例,以 开头的包
java
是 JDK 的一部分(加上扩展)。语言中没有任何内容指定或强制执行它
回答by Suzan Cioc
java
and util
are names of nested packages. java.util
is a path to final package.
java
和util
是嵌套包的名称。java.util
是最终包的路径。
They are directories inside rt.jar
file.
它们是文件内的rt.jar
目录。
rt.jar
file is a zip
archive, you can view it with 7-zip
program.
rt.jar
文件是一个zip
档案,你可以用7-zip
程序查看它。
Scanner
is a Scanner.class
file inside java/util
directory inside rt.jar
Scanner
是Scanner.class
里面的java/util
目录里面的文件rt.jar
import java.util.Scanner
directive just allows you to use Scanner
class name in code without specifying full path to it.
import java.util.Scanner
指令只允许您Scanner
在代码中使用类名而不指定它的完整路径。
import java.util.*
directive allows you to use ALL class names in java.util
without a path.
import java.util.*
指令允许您在java.util
没有路径的情况下使用所有类名。
import static java.util.Scanner.*
directive allows you to use ALL static members inside Scanner
, without a paths. But there are none.
import static java.util.Scanner.*
指令允许您在 内使用所有静态成员Scanner
,而无需路径。但没有。
List of all packages in JRE are here: http://docs.oracle.com/javase/7/docs/api/overview-summary.html
JRE 中所有包的列表在这里:http: //docs.oracle.com/javase/7/docs/api/overview-summary.html
回答by Smartian
the import statement represent a hierarchy
import 语句代表一个层次结构
import java.util.Scanner;
java
is the packageutil
is the subpackage (inside java)Scanner
is the class (inside util)import java.util.*;
java
是包util
是子包(在java里面)Scanner
是类(在 util 内)导入 java.util.*;
The class name could be subtituited with an asterisk,
and that means import all classesin the mentioned subpackage.
类名可以用星号代替,
这意味着导入提到的子包中的所有类。