I asked myself why we didn't import a package while we use String functions such as toUpperCase()
? How they get in there without importing packages?
3 Answers
Java tutorials > Learning the Java Language > Packages:
For convenience, the Java compiler automatically imports three entire packages for each source file: (1) the package with no name, (2) the
java.lang
package, and (3) the current package (the package for the current file)...
Class String is in java.lang
package, hence it is imported automatically per above rule.
The java.lang
package is imported by default. If you take a look at the classes in the package, you'll see that they are very common classes that you end up using frequently, independent of what your program is actually trying to do. Rather than essentially requiring every file to have import statements for these classes, the creators decided to include it for you behind the scenes.
the String object is part of java.lang
which is considered core and is always included (like for example Object
)