Importing the java.lang. package*

Asked

Viewed 329 times

8

Adding the studies in Java I came across a package so much that special, the java.lang, where the author of the book mentions that we do not need to perform the import of that package.

There’s a package special in the Java world called java.lang. This package is special because it is automatically imported. It is still possible to type this package in an import declaration, but you do not need to do That. "OCA: Oracle Certified Associate Java SE 8 Programmer I Study"

I would like to understand why only the package java.lang.* is the only one where we do not need to import when we are developing in Java and what it has of differential with the other packages in which we need to import?

  • 1

    It is because it contains classes essential to the functioning of the JVM, for example, the class Object from which all other classes of the java itself or q vc create must inherit.

  • 5

    @Articunol actually as the inheritance is automatic would not need to import anyway

  • 1

    Without java.lang you couldn’t even do the method main, since its parameter is an array of java.lang.String

1 answer

10


It has all the main functions of framework from Java and you will probably need at least one function of what is in this package in applications that are not absolutely trivial and therefore would have to import it, it is almost impossible to do something useful without using something of it. There is what is most important and it was considered bad to have to make an import almost mandatory in all codes, it would become a bureaucracy. All the other packages already depend a lot on the type of application, it is not any code that you make IO, or use a collection of data, or the utility functions, etc. The other imported ones would automatically increase the surface area of imported codes and increase the chance of conflicts and or having to turn off automatic import.

It is quite curious that Java is considered verbose to adopt this posture. C# preferred to put some very used names in the same language and let you import everything manually, for example almost every application needs to have the using System;.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.