Experiment 5
Experiment 5
Theory
A package in Java is a namespace that organises a set of related classes and interfaces.
Packages serve several important functions:
• Organisation: They help in organising classes and interfaces into a namespace, making it
easier to locate and use them.
• Access Control: They provide access protection to the classes and their members. For
example, classes within the same package can access each other's package-private
members.
• Avoiding Name Conflicts: Packages prevent name conflicts. For example, you can have
two classes with the same name in different packages.
Types of Packages
There are two main types of packages in Java:
• Built-in Packages: These are predefined packages that come with the Java API. For
example:
o java.util: Contains the collections framework, legacy collection classes, event model,
date and time facilities, and miscellaneous utility classes.
o java.io: Provides classes for system input and output through data streams,
serialisation, and file system operations.
o java.lang: Contains fundamental classes, such as String, Math, Integer, System, and
Thread, which are essential to Java programming.
• User-defined Packages: These are custom packages created by the programmer to
group related classes and interfaces together. For example:
o package myutils: Could contain utility classes like FileUtils, StringUtils, etc.
Code
Anish Dharnidhar AI & DS Batch S23 Roll no : 129
Output