Java Interview Questions 9 of 9 - Package Flashcards
What is a package?
The package is a grouping mechanism in which related class files are grouped and made available to other applications and other parts of the same application. So, the package is a collection of related classes and interfaces. The package declaration should be the first statement in a Java class.
What is the purpose of package in Java?
A package is used to encapsulate a group of classes, interfaces and sub-packages. Often, it is a hierarchical structure of storing information. It is easier to organize the related classes and sub- packages in this manner.
A Package also provides access protection for classes and interfaces. A package also helps in removing naming collision.
What is java.lang package?
In Java, java.lang package contains the classes that are fundamental to the design of Java programming language. The most important class in this package is Object class.
It also contains wrapper classes like- Integer, Boolean, Character etc. It provides Math class for mathematical operations.
Which is the most important class in Java?
It is an open-ended question with many answers. In my view, Object class is the most important class of Java programming language. It is the root of all the classes in Java. It provides some very important and fundamental methods.
Is it mandatory to import java.lang package every time?
No. By default, JVM loads it internally.
What is a standard package in Java?
The standard package is the package that contains all libraries or pre-defined class files. There are two types of standard packages:
Core packages (which are starts with Java)
Extension packages (Which are starts with Javax)
Example: For Core packages For Extension packages
- Java.lang Javax.sql
- Java.util Javax.servlet
- Java.awt Javax.servlet.http
- Java.applet Javax.servlet.jsp
- Java.io Javax.ejb
- Java.net Javax.swing
Can you import same package or class twice in your class?
If we import same package multiple times in a class, compiler includes it only once. So neither JVM nor Compiler gives any error/warning on including a package multiple times.
If you have two classes with same name, then you may get name collision on importing the class erroneously.
JVM internally loads the class only one time.
What is a static import in Java?
Static import is similar to normal import declaration. Normal import allows us to import classes from packages without using package qualifier. Static import allows us to import static members from a class without using class qualifier.
What is a user-defined package in Java?
The packages which are created by the users in the Java application development are known as “user-defined package”.
How to create a user-defined package?
The ‘Package’ keyword is used for creating a package in Java. Its syntax is:
package ;
As per industrial standards, the package name should follow the reverse domain naming convention. Example:
package org.companyname.projectname.modulename.submodule;
Can I import the same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or same class multiple times. Neither compiler nor JVM complains about it. JVM will internally load the class only once no matter how many times you import the same class.
Are the imports checked for validity at compile time, like: will the code containing an import such as Java.lang.ABCD be compiled?
Yes, the imports are checked for the semantic validity at compile time. The code containing Java.lang.ABCD will not compile. It will throw an error saying, cannot resolve symbol:
symbol: class ABCD
location: package io
import Java.io.ABCD;
What is the difference between import static com.test.Fooclass and import com.test.Fooclass?
First import is a static import and the second import is normal import of a class. First import allows us to import static members of class.