In this tutorial, we will learn about the java package. Here, we will learn what is package, how to use a package, and how to create a package in Java.
What is a package in Java?
In java, the package is just like a folder that includes classes, interfaces, and sub-packages also. It behaves like a library in Java and it stores pre-defined classes and interfaces in it.
How to Use Package.
In java, packages are referred to using the import keyword. We can call the class of the packages using the import keyword.
Example to import all classes of package.
Here, in the above example, in the end, we are using asterisk(*) sign. It defines that we can use any class or interface of that package.
Another way to use package
Here, in the end, we are using the name of the class instead of the asterisk(*) sign. It defines that we can use only that class of the package in our program.
In the above example
- java is a top level package.
- util is a sub package of java package.
- Scanner is a class of util sub package.
Note: Using asterisk(*), we can call only classes and interfaces, but not package. You have to write package name there.
Some Important Built-In packages.
- java.lang – It is a default package in java, you need not to import this package. It includes primitive types, keywords and classes like System, String etc.
- java.util – It includes utility classe like Scanner, Date & Time and Collections.
- java.io – It stands for Input/Output, includes classes and interfaces for input and output.
- java.awt – It stands for abstract window toolkit, it includes classes for GUI programming. Like Button, Labbel, Menu etc.
- java.sql – It includes classes for Java database connectivity (JDBC).