Professional Documents
Culture Documents
Packages
Packages
Java Programming
Packages
What is Package in Java?
2
• It helps organize your classes into a folder structure and make it easy to
locate and use them. More importantly, it helps improve code reusability.
These are the reasons why you should use packages in Java:
Reusability:
•
While developing a project in java, we often feel that there are few things that we are writing
again and again in our code. Using packages, you can create such things in form of classes inside
a package and whenever you need to perform that same task, just import that package and use the
class.
Better Organization:
•
Again, in large java projects where we have several hundreds of classes, it is always required to
group the similar types of classes in a meaningful package name so that you can organize your
project better and when you need something you can quickly locate it and use it, which improves
the efficiency.
Name Conflicts:
•
We can define two classes with the same name in different packages so to avoid name collision,
we can use packages
Types of packages in Java
4
Based on whether the package is defined by the user or not, packages are divided
into two categories:
User-defined packages
• Built-in Packages
Built-in packages or predefined packages are those that come along as a part of JDK (Java Development Kit) to simplify
the task of Java programmer.
They consist of a huge number of predefined classes and interfaces that are a part of Java API’s. Some of the commonly
used built-in packages are java.lang, java.io, java.util, java.applet, etc.
1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This
package is automatically imported.
3) java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for
Date / Time operations.
5) java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc).
• Choose a name for the package and include a package command as the first
statement in the Java source file. For example, the following statement creates a
package named MyPackage.
• The Source file contains the classes, interfaces, etc you want to include in the
package
Step-1
Package in Java Example
8
Step-2