How does Java decide when to import?

2019-01-06 19:51发布

Why doesn't Java need to import classes like Integer, String, etc. while it needs to import other classes?

8条回答
成全新的幸福
2楼-- · 2019-01-06 19:54

Classes in the java.lang package do not need to be imported (the compiler acts like they are always imported). This package includes core classes such as String, Enum, Runnable, NullPointerException, and of course, the primitive wrapper classes such as Integer and Double.

查看更多
爷、活的狠高调
3楼-- · 2019-01-06 19:55

Integer,String etc classes are present in package java.lang which is default imported.

查看更多
Fickle 薄情
4楼-- · 2019-01-06 19:56

java.lang is in-build, implicitly imported in java, does'nt need to be manually imported

查看更多
放荡不羁爱自由
5楼-- · 2019-01-06 20:04

java.lang package is imported by default, no need to explicitly import it.

查看更多
乱世女痞
6楼-- · 2019-01-06 20:14

Because, they belongs to java.lang.* package. And, it is implicitly import by the compiler. If you do, then it won't complain you.

查看更多
乱世女痞
7楼-- · 2019-01-06 20:15

As it contains very frequently used classes, they have made it optional to import just for your convenience

查看更多
登录 后发表回答