Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 8

1

Java Programming

Packages
What is Package in Java?
2

• Java package is a mechanism of grouping similar type of classes, interfaces,


and sub-classes collectively based on functionality.

• When software is written in the Java programming language, it can be


composed of hundreds or even thousands of individual classes. It makes
sense to keep things organized by placing related classes and interfaces into
packages.

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

• Each package in Java has its unique name


Advantages of using a package in Java
3

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

These are the packages that are defined by the user.


Types of packages in Java
5

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

commonly used built-in packages are:

1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This
package is automatically imported.

2) java.io: Contains classed for supporting input / output operations.

3) java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for
Date / Time operations.

4) java.applet: Contains classes for creating Applets.

5) java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc).

6) java.net: Contain classes for supporting networking operations.


Creating a Package in Java
6

• Creating a package in Java is a very easy task.

• 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

• Compile to create the Java packages


Package in Java Example
7

Step-1
Package in Java Example
8

Step-2

You might also like