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

Java Packages

The document discusses Java packages and APIs. It explains that packages are used to group related classes and avoid naming conflicts. Built-in packages are part of the Java API while user-defined packages can be created. The document provides examples of importing packages and creating user-defined packages in Java code.

Uploaded by

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

Java Packages

The document discusses Java packages and APIs. It explains that packages are used to group related classes and avoid naming conflicts. Built-in packages are part of the Java API while user-defined packages can be created. The document provides examples of importing packages and creating user-defined packages in Java code.

Uploaded by

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

Java Packages & API

A package in Java is used to group related classes. Think of it as a folder in a file


directory. We use packages to avoid name conflicts, and to write a better maintainable
code. Packages are divided into two categories:

 Built-in Packages (packages from the Java API)


 User-defined Packages (create your own packages)

Built-in Packages in Java

Packages https://www.youtube.com/watch?v=OkJvlLn7u3I (12 Mins)

Java application programming interface (API) is a list of all classes that are part of the Java development
kit (JDK). It includes all Java packages, classes, and interfaces, along with their methods, fields, and
constructors. These prewritten classes provide a tremendous amount of functionality to a programmer.
A programmer should be aware of these classes and should know how to use them.

Syntax: import java.packagename.*;

import java.util.*;

This is the import statement imports all the classes in the API’s java.util package and makes them
available to the programmer. If you check the API and look at the classes written in the java.util package,
you will observe that it includes some of the classes that are used often, such as Arrays, ArrayList,
Formatter, Random, and many others. Another Java package that has several commonly used classes is
the java.lang package. This package includes classes that are fundamental to the design of Java
language. The java.lang package is automatically imported in a Java program and does not need an
explicit import statement. Please note that some of the classes that we use very early in Java
programming come from this package. Commonly used classes in the java.lang package are: Double,
Float, Integer, String, StringBuffer, System, and Math.
User Defined Packages in Java
One of the most innovative features of Java is the concept of packages. Packages in Java are a
way to encapsulate a group of classes, interfaces, enumerations, annotations, and sub-
packages. Conceptually, you can think of java packages as being similar to different folders on
your computer.

User-defined packages are those which are developed by users in order to group related
classes, interfaces and sub packages. With the help of an example program, let’s see how to
create packages, compile Java programs inside the packages and execute them.

Creating a Package in Java


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. The java source file can
contain the classes, interfaces, enumerations, and annotation types that you want to include in
the package. For example, the following statement creates a package named MyPackage.

Including a Class in Java Package


To create a class inside a package, you should declare the package name as the first statement
of your program. Then include the class as part of the package. But, remember that, a class can
have only one package declaration

You might also like