In this article, we will learn about what package is in java, what are different built-in packages in java,how to create user-defined packages in java, and how to write package program in java.
What is a java package?
We can say a java package is a group that contains similar types of classes, sub-packages, and interfaces. In Java, we have two types of packages one is built-in packages and user-defined packages. We have some built-in packages like util, java, io, net, sql, java, etc. In this article, we will learn about these built-in packages and we will also learn how to create a user-defined package.
What are the advantages of a java package?
- Java packages are used to proved access protection
- Java packages are used to eliminate naming collision
- Java packages are used to classify the classes and the interfaces into categories
Let’s see how the hierarchy of the java build-in package looks like
In the above image, we can see that there is one main package java that contains sub-packages like io, lang, net, util, etc. And these sub-packages contain various classes. For example, the io sub-package contains FileReader.class, Writer.class, and many more. The lang sub-package contains Thread.class, System.class, and many more. The net sub-package contains Socket.class, URL.class, and many more. The util sub-package contains Scanner.class, Random.class, and many more.
Simple package program in java:
Let’s see how to write a simple package program in java using built-in packages of java.
import java.util.Scanner; class PrepBytes { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int num=sc.nextInt(); System.out.println("The Number is "+num); } }
Output: The Number is 10
In the above program, we have imported the Scanner class of sub-package util which belongs to package java. We have used this Scanner class to take input from the user. In the output, we can see that number entered by the user is printed.
Now, let’s see how we can write our user-defined package program in java.
package mypackage; class PrepBytes { public static void main(String[] args) { System.out.println("This is user-defind package"); } }
In the above program, we have given the package name mypackage using the keyword package. We can use this package in another program of java using the import package name. You can compile the above program using the below steps if you are not using IDE.
Syntax: javac java_file_name
For example: javac PrepBytes.java
You can use the above steps to compile your program using the command prompt. After compiling, we can use the above package in other java programs. Following the below steps, you can run the above java program.
Syntax: java package_name.class_name
For example: java mypackage.PrepBytes
Output: This is user-defined package
Using the above syntax, we can run a java program.
Ways to access a package from another package:
There are mainly three ways to access a package from another package.
- Using import package_name.*
- Using import package_name.class_name;
- Using the fully qualified name
1) Using import package_name.*;
When we want to import all the classes and the interfaces to our java program from the package we can use this method. * means the java compiler will import all the classes and the interfaces into the current program. The import keyword will make all the classes and interfaces accessible to the current package. Let’s understand this method by writing a package program in java.
import java.util.*; class HelloWorld { public static void main(String[] args) { Date d=new Date(); System.out.println(d); Random r=new Random(); int num=r.nextInt(100); System.out.println(num); } }
In the above program, we have imported all the classes of sub-package util. In the program, we used the Date class of the package java.util and we also used the Random class from the same package. In the output, we can see that we don’t get any error even if we didn’t import the Date and Random classes separately it’s because we imported all the classes and interfaces of the package using the * sign.
2) Using import package_name.class_name
We will use this method when we want to import a particular class or interface from the package. This method is very useful when we don’t want to import all the classes and interfaces and we want to use a specific class or interface. The import keyword will all the specified classes and interfaces accessible to the current package. Let’s understand this method by writing a package program in java.
import java.util.Date; class HelloWorld { public static void main(String[] args) { Date d=new Date(); System.out.println(d); } }
In the above program, we have imported a class Date using the method of importing a specific class. In the output, we can see that we can use the Date class in the program and we do not get any errors. Now, let’s try to use the Random class without importing it.
import java.util.Date; class HelloWorld { public static void main(String[] args) { Date d=new Date(); System.out.println(d); Random r=new Random(); int num=r.nextInt(100); System.out.println(num); } }
In the output, we can see that the above program throws an error because we have not imported the Random class.
3) Using the fully qualified name
In this method, we will specify the full name of the class with its sub-package name and package name. To make the class accessible to the program we can give a fully qualified name. Let’s understand this method by writing a package program in java.
class HelloWorld { public static void main(String[] args) { java.util.Date d=new java.util.Date(); System.out.println(d); } }
In the above program, we did not import any classes or interfaces using the above two methods. In this method, we give fully qualified names to access the classes or interfaces rather than importing them on the top. One disadvantage of this method is that every time we want to use the classes or interfaces we need to give a fully qualified name every time.
The flow of the package in java:
In the above image, we can see a flow of the java package. First, java packages are imported and after that, we can import classes and interfaces belonging to that package.
What is a sub-package in java?
If one package is inside the other package then that package is called a sub-package. The main purpose of this sub-package is to divide the classes and interfaces into categories.
For example, in java language, there is one main package java that contains many sub-packages like io, lang, net, util, etc. These all sub-packages contain the classes and interfaces with the same categories.
For example, the io sub-package contains classes like Reader and Writer which are used for input and output, and the net sub-package contains classes like Socket and URL which are used for networking. The util class contains classes like Scanner and Random which are used to create an applet.
Let’s see an example of how to write a sub-package program in java.
package com.prepbytes.core; class HelloWorld { public static void main(String[] args) { System.out.println("Welcome to sub-package program in java"); } }
Output: Welcome to the sub-package program in java
In the above program, we have defined the package name at the top of the program. We have given package name like com.prepbytes.core. For this program, our main package is com which contains sub-package prepbytes. Inside the sub-package, we have created the class HelloWorld. Now, if we see the structure of the package then first, we have to call main package com after that we have to call sub-package prepbytes and after that, we can use the class HelloWorld in another program. This is a standard technic for creating user-defined packages because it is more readable and easy to use. We will see the above structure in every industry-level coding standard.
Other Java Programs
Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java