Home > Java > javaTutorial > body text

Default Package in Java

PHPz
Release: 2024-08-30 15:10:28
Original
1004 people have browsed it

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 Tests

How does the Default Package work in Java?

To 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.

Examples to Implement Default Package in Java

Let us see examples that use the default package and see how it works.

Example #1

Code:

public class Main {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Copy after login

Output:

Default Package in Java

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.

Example #2

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));
}
}
Copy after login

Output:

Default Package in Java

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));
}
}
Copy after login

Output:

Default Package in Java

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.

Example #3

Code:

import java.lang.System.*;
public class PackageDemo
{
public static void main(String args[])
{
System.out.println("Welcome to EduCBA");
}
}
Copy after login

Output:

Default Package in Java

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.

Conclusion

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.

The above is the detailed content of Default Package in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!