How does Java decide when to import?

后端 未结 9 1373
时光取名叫无心
时光取名叫无心 2020-11-27 20:55

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

相关标签:
9条回答
  • 2020-11-27 21:05

    every class in java is in a package and if no package is defined then it is understood as in default package. And at the top of the package is java.lang.* so we don't need to import it an require to import other classes.

    0 讨论(0)
  • 2020-11-27 21:08

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

    0 讨论(0)
  • 2020-11-27 21:12

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

    0 讨论(0)
  • 2020-11-27 21:15

    There is an implicit import of java.lang.*.

    From the Java specification:

    A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.

    0 讨论(0)
  • 2020-11-27 21:18

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

    0 讨论(0)
  • 2020-11-27 21:19

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

    0 讨论(0)
提交回复
热议问题