String is present in package java.lang which is imported by default in all java programs.
String is present in package java.lang which is imported by default in all java programs.
Everything in the java.lang package is implicitly imported (including String) and you do not need to do so yourself. This is simply a feature of the Java language. ArrayList and HashMap are however in the java.util package, which is not implicitly imported.
The package java.lang mostly includes essential features, such a class version of primitives, basic exceptions and the Object class. This being integral to most programs, forcing people to import them is redundant and thus the contents of this package are implicitly imported.
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.langpackage, 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.