Prev Next

Java / Package

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. What is a Java package?
Posted on Apr 1, 2016 by Senthil Kumar.

Package is a collection of related classes and interfaces. Related classes will have the package defined using package keyword.

package packageName.subpackageName;

Any java class/interface will have the package declaration as the first statement.

package is optional however it is common practice to place the java class under a package.

2. Which package is auto imported by default in any java class?
Posted on Apr 1, 2016 by Senthil Kumar.

java.lang package and no import statement is necessary.

3. Is there a performance impact due to a large number of import statements that are unused?

No. the unused imports are ignored if the corresponding imported class/package is not used.

4. Difference between Java IO and NIO packages.

IO.NIO.
Stream oriented.Buffer oriented.
Blocking IO.Non blocking IO.
No Selectors.Selectors available.

5. Difference between import and static import.

The import allows the Java programmer to access classes of a package without package qualification whereas the static import feature allows accessing the static members of a class without the class qualification.

The import provides accessibility to classes and interfaces whereas static import provides accessibility to static members of the class.

6. Can I import same package/class twice?

Yes. One can import the same package or same class multiple times. JVM will internally load the class only once no matter how many times one imports the same class.

7. Which package is always imported by default?

The java.lang package is always imported by default.

8. Does importing a package imports the sub packages as well?

No. One will have to import the sub packages explicitly.

9. Should package be the first statement in a Java class?

Yes. Otherwise it will produce compilation error.

10. Explain Java9 module.

Java module is a new feature in Java 9 via the Java Platform Module System (JPMS). JPMS Is also referred to as Java Jigsaw or Project Jigsaw.

A Java Module is a mechanism to package up your Java application and Java packages into Java modules. A Java module can specify which of the Java packages it contains that should be visible to other Java modules using this module. A Java module must also specify which other Java modules is requires to do its job.

«
»
Methods

Comments & Discussions