11

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 3

23

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.

3

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.

3

the String object is part of java.lang which is considered core and is always included (like for example Object)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.