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

Java Packages

Uploaded by

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

Java Packages

Uploaded by

shalinim8387
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

JAVA PACKAGES

Packages

• It is a logical organization of related classes. We can logically organize java classes into
packages.
• It means putting all the related classes in one place that is a package.
• Ex: In a banking application we put all the accounting related classes in one package.
• They must be accessed through their package names.
why do we need them?
In programming it is often helpful to group related pieces of a program together, and it is accomplished
using packages.
 In java, programmers can create several classes and Interface. After creating the classes and interface, it
is better if they are divided into some groups depending on their relationship.
 Thus, the classes and Interface which handle similar or same task are put into the same directory or
folder, which is also known as package.
 Packages acts as an “container” for classes. A package represents a directory that contains related group
of classes and Interface.
Classes defined within a package can be made private the package so that is cannot be accessed by code
outside the package. Thus it provides classes encapsulation.
Types Of Packages
There are basically only 2 types of java packages.
They are as follows :
• Java API Packages
• User defined Packages
Java API Package
It contains all the built in classes provided by java. They are organized as the collection of classes
into packages based on their functionality. These classes inside the packages are already defined
and we can use them by importing relevant packages in our program.
It is easier to remember name of the package rather than classes.

Java has an extensive library of packages, a programmer need not think about logic for doing any
task. For everything, there are methods available in java and that method can be used by the
programmer without developing the logic on his own. It makes programming easy
Java API Packages And Their Classes

• Java.lang : Language Support classes. These are the classes that java compiler itself uses and
therefore they are are automatically imported. They include classes for primitive types such as
strings, maths function, threads and exeptions.
• Java.util : Language utility class such as vector, hash table, random numbers, date, scanner etc.
• Java.io : Input/Output support classes. They provide facilities for the input and output of the
data. Some classes are Bufferedreader, Bufferedwriter, ByteArrayInputStream etc.
• Java.awt : Set of classes for implementing graphical user interface. They include classes for
windows, buttons, list, menus and so on.
• Java.net : Classes for networking. They include classes for communication with local computers
as well as with internet servers. Some of the classes are URL class, URLConnection class, socket
class and so on.
• Java.applet : classes for creating and implementing applets , some of the classes are
Using API packages
Every time we use want to use a package we call it using import command.
When we uses import command we refer to a class by its full package name.
Ex: (i) import java.util.Vector; // to get a specific class
Vector v = new Vector();
(ii) import java.util.*; // use asterisk(*) to get all the classes in the package
Scanner sc = new Scanner();
Vector v = new Vector();

Note: packages and class names are case sensitive.


\\program to find smallest of 2 values and find their square roots

import java.util.*;
import java.io.*;
import java.lang.Math;

public class example1 {


public static void main(String args[]) throws IOException {
Scanner sc= new Scanner(System.in);
int a,b;
System.out.println("Enter the values a and b ");
a=sc.nextInt();
b=sc.nextInt();
if(a<b)
System.out.println("A is the smallest");
else
System.out.println("B is the smallest");
double sqrt1=Math.sqrt(a);
double sqrt2=Math.sqrt(b);
System.out.println("Sqrt of a is "+sqrt1);
System.out.println("Sqrt of b is "+sqrt2);
}}
User Defined Packages
The users of the Java language can also create their own packages. They are called user defined
packages. User defined packages can also be imported into other classes and used exactly in the
same way as the built in packages.
o A user-defined package is defined using the keyword Package.
o that is,
package pkgname;

o We can also create hierarchy of a package using periods between them


oEx: package pkg1. pkg2. ……..pkgN;
Creating User-Defined packages:
Step 1: Create a program which is part of package called pack1.

package pack1; // name of the package is pack1.


public class packageExample {
public void packMethod()
{
System.out.println(“I am packMethod() in pack1.packageExample”);
}}
Creating user defined packages……..

Step 2 : place the file packageExample.java in C:\ and compile the program. We have to use –d option to create the

pack1 directory automatically.

Step 3 : Create another file called packageDemo.java in pack2 package .


package pack2; // name of the package is pack2.
import pack1.*;
public class packageDemo {
public static void main(String args[]){
packageExample obj = new packageExample();
obj.packMethod();
}}
Creating user defined packages……..
Step 4 : place this file also in C:\ and compile the program using –d option.
Then pack2 directory will be created and class file is also copied to that directory
automatically.
Step 5 : The completion of both the classes will be completed. Now execute the
packageExample class , as we execute any other java program.
i.e, java pack2.packageDemo

You might also like