Java import statement syntax

2019-04-07 00:51发布

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,

.util is the name of the package

What is java or javax or whatever would stand before the first period in general?

UPDATE:

I also found this picture:

http://www.javatpoint.com/package

Is it true?

标签: java import
7条回答
▲ chillily
2楼-- · 2019-04-07 01:18

the import statement represent a hierarchy

import java.util.Scanner;
  • java is the package
  • util is the subpackage (inside java)
  • Scanner is the class (inside util)

    import java.util.*;

The class name could be subtituited with an asterisk,
and that means import all classes in the mentioned subpackage.

查看更多
成全新的幸福
3楼-- · 2019-04-07 01:19

java and util are names of nested packages. java.util is a path to final package.

They are directories inside rt.jar file.

rt.jar file is a zip archive, you can view it with 7-zip program.

Scanner is a Scanner.class file inside java/util directory inside 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.* directive allows you to use ALL class names in java.util without a path.

import static java.util.Scanner.* directive allows you to use ALL static members inside Scanner, without a paths. But there are none.

List of all packages in JRE are here: http://docs.oracle.com/javase/7/docs/api/overview-summary.html

查看更多
何必那么认真
4楼-- · 2019-04-07 01:21

A few points:

  • the package name is java.util, not util. "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
查看更多
Animai°情兽
5楼-- · 2019-04-07 01:22

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


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.

Hope this helps

查看更多
Rolldiameter
6楼-- · 2019-04-07 01:28

import java.util.Scanner says.

  1. Look in the package java.
  2. Within that look in the package util.
  3. Within that find the class Scanner.
  4. 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.

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.

查看更多
走好不送
7楼-- · 2019-04-07 01:35

Per the 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.

So you can glean from that:

  • java is a package with no classes, only subpackages.
  • util is a subpackage of java whose fully qualified name is java.util.
  • util does not denote a package, java.util does.

"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.

You can think of packages 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).

查看更多
登录 后发表回答