Packages are a mechanism in Java that wraps a group of classes, sub-packages, and different interfaces. They group all related objects like classes, interfaces, etc., and helps in managed ways of providing access and namespaces. The default package is a collection of java classes whose source files do not contain and package declarations. These packages act as the default package for such classes. It provides the ease of creating small applications when the development of any project or application has just begun.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsTo work with the package, we should have a package name and directory structure. These two attributes are closely coupled. If the package name is office.employee.cs, then there will be three directories. They will be office, employees, and cs. This structure would be such that cs are present in employees and employees are part of the office. The main directory office would be accessible to the Classpath variable. The packages which contain these classes are supposed to have reverse order of domain name. For example, we can name the packages as an office.employees.cd, office.employees.admin, office.transport.drivers, etc. The packages hence work in a hierarchy. If there is not package defined for a class, then the default package comes into the picture. The package can be assigned to any class which does not have any package defined. There is an unnamed package that does not have any name. The class name is placed into a default package if you do not choose the ‘package’ statement while creating your class definition. Java compiler will automatically take the package name for this class.
Let us see examples that use the default package and see how it works.
Code:
public class Main { public static void main(String[] args) { System.out.println("Hello world"); } }
Output:
Explanation:This is the most basic program in Java. Probably the first one you would have ever written when you started learning Java. This program does not have any package mentioned; hence it takes the default package, which is unnamed. It does not throw any error. The compiler chooses the default package, and the code gives the required output. The below snippet shows the output which is expected. It prints the line which says Hello World.
Code:
package letsdosomemath; public class Calculate { public int add(int a, int b){ return a+b; } public static void main(String args[]){ Calculate cal = new Calculate(); System.out.println("The addition of a and b is " + cal.add(10, 20)); } }
Output:
Explanation:The above program uses a user-defined package. The package is declared in this program. The first line declares the package with the name letsdosomemath. A package can always be declared at the beginning of your program before the class begins. Also, a class can have only one package declared. This package is declared and can be used for the programs to follow. The output of this program will be the addition of two integers which are defined. It will call the Calculate class, and the add() function will return the value of the addition of two integers that are passed to this function.
Now this declared package can be used in another program easily.
Code:
import letsdosomemath.Calculate; public class Letstry{ public static void main(String args[]){ Calculate cal = new Calculate(); System.out.println(cal.add(100, 200)); } }
Output:
Explanation:We now use the above package in our next program. We use it by importing it explicitly. As the import statement is mentioned, it will not take the default package. We have specified a package to be used, and hence the compiler will go and look for this package. The package specified here is doing the work of adding two integers. Hence we will not have to write the functionality of adding two integers again. The Lottery class will directly create a new object for the Calculate class. The object created is cal. Cal will refer to the package and directly run the function add(). It will return to take the values when the cal.add function is called with integers 100 and 200. The add() function will return the required values, which is 300. Here we did not need to mention the add() function details again. Just by importing the user-defined package we created, we could add the given two integers. We get the desired output which can be seen in the above screenshot.
Code:
import java.lang.System.*; public class PackageDemo { public static void main(String args[]) { System.out.println("Welcome to EduCBA"); } }
Output:
Explanation:The above code imports the inbuilt class of java.lang.System. It follows the hierarchy which we have mentioned earlier. The system refers to the functions present in it. The system is a part of sub package lang, which is, in turn, a part of java. The system can be said as a class present in subpackage lang. This package helps us in using the system functions. As we import this package, we are able to use System.out.println. Also, as we have specified a package to be imported, the default package will not be selected in this case. The output of the above code will be as below.
Note:This is a classic example as we are not using any specific functions; the default package consists of the system functions. Hence, if we remove the import statement at the beginning of the code, the program will still work fine. The default package has this functionality.Hence, the default package is a set of functionalities that provide a basic setup that is needed to run programs. If any specific package is not chosen, the compiler will choose this default package, enabling the Java code to function better. We can also use the built-in packages or create user-defined packages, which can be used as and when required. They will be needed to declared and then imported into the program where it is required. Packages make the code reusable and efficient. Name conflicts can be avoided, and the code is also well organized.
以上是Default Package in Java的詳細內容。更多資訊請關注PHP中文網其他相關文章!