But my question is if java.lang.* is imported by default then why is the Math class not imported and the abs method not available?
因为不是.
因为那是Java工作的方式.包中类的隐式(或显式)通配符导入仅导入类.它也不会静态导入类成员.
如果要引用类的所有静态成员而又不限定它们的资格,则应使用通配符静态导入;例如
import static java.lang.Math.*;
另外,您可以静态导入单个成员;例如
import static java.lang.Math.abs;
他们为什么用这种方式定义Java?
好吧,最有可能的理由是隐式导入使读取代码变得更加困难.如果默认情况下会导入abs之类的方法,则您需要了解它们的全部含义以及从…处导入的位置,以便了解代码的真实含义.
值得一提的是,静态导入仅在Java 5中添加.在此之前,没有Math资格就无法引用Math.abs.
If you just import the class not its static members then what are you getting when you import it?
您只得到类名.例如:
import java.util.HashMap;
允许我编写新的HashMap()而不是新的java.util.HashMap()等.这很重要. (想象一下,如果您总是必须用全名来引用类….)