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

Packages in Java (1) - 1

Packages in Java allow developers to organize related classes and interfaces. There are built-in packages provided by Java, such as java.lang and java.util, which contain fundamental classes. Developers can also create their own user-defined packages. Packages provide advantages like modularity, access control, and code reusability. They allow for easier management of larger codebases but can also increase complexity if not organized properly.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Packages in Java (1) - 1

Packages in Java allow developers to organize related classes and interfaces. There are built-in packages provided by Java, such as java.lang and java.util, which contain fundamental classes. Developers can also create their own user-defined packages. Packages provide advantages like modularity, access control, and code reusability. They allow for easier management of larger codebases but can also increase complexity if not organized properly.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

PACKAGES IN JAVA

WHAT IS PACKAGES IN JAVA

• A set of classes and interfaces grouped together are known as Packages in JAVA.
• Every class is a part of a certain package.
• When you need to use an existing class, you need to add the package within the Java program.

YOU CAN CATEGORIZE PACKAGES INTO:


IN-BUILT PACKAGES

USER-DEFINED PACKAGES
IN-BUILT PACKAGES

• In java, we already have various pre-defined packages and these packages contain large numbers of
classes and interfaces that we used in java are known as Built-in packages.

• for example, lang, io, util, SQL, etc. Java provides various built-in packages
• java.sql : Provides the classes for accessing and processing data stored in a database. Classes like
Connection, Driver Manager, Statement, etc. are part of this package.

java.lang : Contains classes and interfaces that are fundamental to the design of the Java programming
language. Classes like String, StringBuffer, System, Math, Integer, etc. are part of this package.

java.util : Contains the collections framework, some internationalization support classes, properties,
random number generation classes. Classes like ArrayList, LinkedList, HashMap, Calendar, Date, Time
Zone, etc. are part of this package.

• java.net : Provides classes for implementing networking applications. Classes like Authenticator, HTTP
Cookie, URL, URLConnection, URLEncoder, URLDecoder, etc. are part of this package.
USER DEFINED PACKAGES
• User-defined packages in Java are packages that are created by developers to organize their own classes
and related code. These packages are not part of the Java Standard Library and are specific to the
developer's project or application.

• To create a user-defined package, you can use the “package” keyword followed by the package name at
the top of your Java source file.

• For example, if you want to create a package called "com.mycompany.myapp", you would include the
following line at the beginning of your Java file.

• package com.mycompany.myapp;
EXAMPLE
package pkg;
// Class
public class gfg {
public void show()
{
System.out.println("Hello ! How are you?");
}
public static void main(String args[])
{
gfg obj = new gfg();
obj.show();
}
}
ADVANTAGES OF PACKAGES
• Organization and Modularity: Packages provide a way to organize related classes and interfaces
together. This makes it easier to locate and manage your code, especially in larger projects. It promotes
modularity, allowing you to break down your code into smaller, more manageable units.

• Access Control: Packages allow you to control access to classes and members using access modifiers like
public, private, and protected. prevents unwanted access to your code.

• Reusability: Packages promote code reusability. You can create reusable components within a package
and use them in different projects or modules. This saves development time and effort.

• Collaboration: Packages facilitate collaboration among developers working on the same project. By
organizing code into packages, it becomes easier to share and understand each other's code.
DISADVANTAGES OF PACKAGES
• Increased Complexity: Introducing packages can add complexity to your codebase, especially if not
properly organized or documented. It may require additional effort to understand the relationships and
dependencies between different packages.

• Potential for Confusion: If package names are not chosen thoughtfully or are too generic, there may be
a risk of name clashes or
confusion when integrating code from different sources.

• Package dependency: if packages have strong dependencies on each other , it can create challenges
when making changes or update. A change in one package may require modifications in multiple
dependent packages , leading to increased maintenance effort.
THANK
YOU

You might also like