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条回答
Bombasti
2楼-- · 2019-04-07 01:37

Classes in Java are identified by a fully qualified name consisting 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:

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.

The process is actually a bit more complicated, as inner/nested classes and interfaces may be involved, but you get the idea.

查看更多
登录 后发表回答