0% found this document useful (0 votes)
2 views

Packages in Java

Packages in Java are physical directory structures that encapsulate related classes and interfaces, promoting code reusability and avoiding naming collisions. There are 14 built-in packages in Java, including java.lang, java.io, and java.util, which provide essential classes for various functionalities. User-defined packages can also be created, and every class must belong to a package, either specified by the user or defaulting to an unnamed package.

Uploaded by

amazinganish11
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Packages in Java

Packages in Java are physical directory structures that encapsulate related classes and interfaces, promoting code reusability and avoiding naming collisions. There are 14 built-in packages in Java, including java.lang, java.io, and java.util, which provide essential classes for various functionalities. User-defined packages can also be created, and every class must belong to a package, either specified by the user or defaulting to an unnamed package.

Uploaded by

amazinganish11
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Package is a physical directory structure containing

correlated classes and interfaces. Packages can be


considered as data encapsulation (or data-hiding).
Advantage of using packages
 Reusability of the code as we can import packages wherever
needed.
Inheritance provides reusability of the code. But it is limited to
the particular class. With packages shareability of the code
becomes global.
 Avoids naming collision – Two classes with same name can
exist in two different packages.
 Managing multiple classes becomes effective and efficient.

Built-in Packages
There are 14 built-in packages in Java. These packages consist of a
large number of classes which are a part of Java API. Some of the
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 hence called as default package.
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.
Other library packages are beans, math, nio, rmi, security, sql, text,
time
User-defined packages
These are the packages that are defined by the user. First, we
create the package e.g. myPackage. Then create
the MyClass inside it with the first statement being the package
myPackage.
Important points:
1. Every class is part of some package.
2. If no package is specified, the classes in the file goes into a
special unnamed package (the same unnamed package for all
files).
3. All classes/interfaces are part of some package. Multiple classes
can specify the same package name.

https://www.youtube.com/watch?v=6U_1IbgJlEE

You might also like