0% found this document useful (0 votes)
10 views4 pages

Understanding Java Packages Explained

A Java package is a collection of related classes, interfaces, and sub-packages that helps in organizing code and preventing naming conflicts. Packages can be built-in, such as java.lang and java.util, or user-defined, created using the 'package' keyword. They provide access protection and facilitate code reuse, with the ability to import specific classes or entire packages using the import statement.

Uploaded by

guptahani75
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Understanding Java Packages Explained

A Java package is a collection of related classes, interfaces, and sub-packages that helps in organizing code and preventing naming conflicts. Packages can be built-in, such as java.lang and java.util, or user-defined, created using the 'package' keyword. They provide access protection and facilitate code reuse, with the ability to import specific classes or entire packages using the import statement.

Uploaded by

guptahani75
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Packages In Java

A java package is a group of similar types of classes, interfaces and sub-packages.

Package in java can be categorized in two form, built-in package and user-defined package.

There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.

Package in Java is a mechanism to encapsulate a group of classes, sub packages and


interfaces.

Advantage of Java Package/ Purpose of Package

1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.

2) Java package provides access protection- Java packages provide access protection through the
use of access modifiers. These access modifiers control the visibility of classes, methods, fields,
and constructors within and outside the package.

3) Java package removes naming collision- Two classes with same name can exist in
different packages.

A package is a container of a group of related classes where some of the classes are accessible
are exposed and others are kept for internal purpose. We can reuse existing classes from the
packages as many time as we need it in our program.
Subpackages: Packages that are inside another package are the subpackages. These are not
imported by default, they have to imported explicitly.

Importing Packages:

You can import a package or a specific class from a package using the import keyword.

Example:

import [Link];
import [Link];

You can also import all classes from a package using *.

Example :
import [Link].*;
util is a subpackage created inside java package.

Types of packages:
Built-in Packages
These packages consist of a large number of classes which are a part of Java [Link] of the
commonly used built-in packages are:
1. [Link]: Contains language support classes(e.g classes which defines primitive data
types, math operations). This package is automatically imported.
2. [Link]: Contains classes for supporting input / output operations.
3. [Link]: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4. [Link]: Contains classes for creating Applets.
5. [Link]: Contain classes for implementing the components for graphical user interfaces
(like button , ;menus etc).
6. [Link]: Contain classes for supporting networking operations.

User-defined packages:

These are the packages that are defined by the user.


To create your own package use the “package” keyword.

Note: The package name should be written in lowercase to avoid conflict with the class
name.

package mypack;

class MyPackageClass {
public static void main(String[] args) {
[Link]("This is my package!");
}
}

OUTPUT

This is my package!

Save the file as [Link], and compile it:

C:\Users\Your Name>javac [Link]

Then compile the package:

C:\Users\Your Name>javac -d . [Link]

This forces the compiler to create the "mypack" package.

The -d keyword specifies the destination for where to save the class file. You
can use any directory name, like c:/user (windows), or, if you want to keep
the package within the same directory, you can use the dot sign ".",

You might also like