Java Packages and Their Importance in Programming

 
PACKAGE
 
What is Package
 
P
A
C
K
A
G
E
 
i
n
 
J
a
v
a
 
i
s
 
a
 
c
o
l
l
e
c
t
i
o
n
 
o
f
 
c
l
a
s
s
e
s
,
 
s
u
b
-
p
a
c
k
a
g
e
s
,
 
a
n
d
 
i
n
t
e
r
f
a
c
e
s
.
 
I
t
 
h
e
l
p
s
 
o
r
g
a
n
i
z
e
 
y
o
u
r
c
l
a
s
s
e
s
 
i
n
t
o
 
a
 
f
o
l
d
e
r
 
s
t
r
u
c
t
u
r
e
 
a
n
d
 
m
a
k
e
 
i
t
 
e
a
s
y
 
t
o
l
o
c
a
t
e
 
a
n
d
 
u
s
e
 
t
h
e
m
.
 
M
o
r
e
 
i
m
p
o
r
t
a
n
t
l
y
,
 
i
t
 
h
e
l
p
s
i
m
p
r
o
v
e
 
c
o
d
e
 
r
e
u
s
a
b
i
l
i
t
y
.
Each package in Java has its unique name and
organizes its classes and interfaces into a separate
namespace, or name group.
Although interfaces and classes with the same name
cannot appear in the same package, they can appear
in different packages. This is possible by assigning a
separate namespace to each Java package.
 
Java API Packages
 
Java API provides a large number of classes grouped
into different packages according to functionality.
Java
lang
applet
net
awt
io
util
 
Cont.
 
java.lang: 
Language support classes. These
are the classes that Java compiler itself uses
and therefore they are automatically
imported. They include classes of primitive
types, strings, math functions, threads and
exceptions.
java.util:
 
Language utility classes such
as vector, hash tables, random numbers,
data etc.
java.io: 
Input/output support classes. They
provide facilities for the input and output of
data
 
Cont.
 
java.awt:
 set of classes for implementing
graphical user interface. They include
classes for windows, buttons, lists, menus
and so on.
java.net:
 
Classes for networking. They
include classes for communicating with
local computers as well as with internet
servers.
java.applet:
 Classes for creating and
implementing applets.
 
H
i
e
r
a
r
c
h
i
c
a
l
 
r
e
p
r
e
s
e
n
t
a
t
i
o
n
 
o
f
j
a
v
a
.
a
w
t
.
p
a
c
k
a
g
e
java
Color
Graphics
Font
Image
 
java
 
awt
 
Type of Package
 
Package in java can be categorized in two form, built-
in package and user-defined package.
There are many built-in packages such as java, lang,
awt, javax, swing, net, io, util, sql etc.
Here, we will have the detailed learning of creating
and using user-defined packages.
 
Advantage of Java 
Package
 
Java package is used to categorize the classes and
interfaces so that they can be easily maintained.
 Java package provides access protection.
Java package removes naming collision.
 
Cont..
 
How to Create a Package?
 
Creating a package is a simple task as follows
Choose the name of the package
Include the package command as the first line
of code in your Java Source File.
The Source file contains the classes, interfaces,
etc you want to include in the package
Compile to create the Java packages
 
Syntax
 
Package packagename; 
// Name of package to be created
Create Class
Package p1;
class
 
firstp {
    
public
 
static
 
void
 
main(String[] args)
    
{
       
System.out.println(“
This Is The First Class in Package"
);
    
}
}
 
Compile
 
Save class with firstp.java
Compile -> javac firstp.java
Command for create a package
Java –d . Firstp.java
  //
The 
“.” 
operator represents the current working
directory.
Run file
Java p1.firstp
 
Compile 
T
he Same 
F
ile 
U
sing .. Dots
 
Javac –d .. Firstp.java
//Here “..” indicates the parent directory. In our
case file will be saved in parent directory which is C
Drive
 
Create a Subpackage
 
package p1.p2;
class c1{
    public void m1()
    {
     System.out.println("m1 of c1");
    }
}
 
Compile
 
Javac –d .. first.p.java
 
Run
 
Java p1.p2.firstp
 
Use of Userdefined Package
 
package letmecalculate;
 public class division {
      public int div(int a, int b)
      {
           return a/b;
      }
  }
Compile :- Javac –d . division.java
 
Cont..
 
import letmecalculate.*;
class arthmatic {
      public static void main(String args[])
        {
           letmecalculate.division obj = new
letmecalculate.division();
           System.out.println("Division of two number is-:" +
            obj.div(10,5));
        }
  }
 
17
 
Insert Multiple classes in a Package
 
Package letmecalculate;
 
Class classname1;  Compile
Javac –d . Classname.java
Class classname2;  Compile
Javac –d . Classname.java   etc
 
18
Slide Note
Embed
Share

An exploration of Java packages, their role in structuring code, organizing classes, and enhancing code reusability. Learn about built-in and user-defined packages, the Java API's package organization, and the different types of packages available in Java programming. Discover how packages help in categorizing classes, interfaces, and sub-packages, making it easier to manage and locate resources within a Java project.

  • Java Programming
  • Packages
  • Code Organization
  • Java API
  • Reusability

Uploaded on Jul 19, 2024 | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Amity School of Engineering & Technology PACKAGE

  2. Amity School of Engineering & Technology What is Package PACKAGE in Java is a collection of classes, sub- packages, and interfaces. It helps organize your classes into a folder structure and make it easy to locate and use them. More importantly, it helps improve code reusability. Each package in Java has its unique name and organizes its classes and interfaces into a separate namespace, or name group. Although interfaces and classes with the same name cannot appear in the same package, they can appear in different packages. This is possible by assigning a separate namespace to each Java package.

  3. Amity School of Engineering & Technology Java API Packages Java API provides a large number of classes grouped into different packages according to functionality. Java lang util io awt net applet

  4. Cont. java.lang: Language support classes. These are the classes that Java compiler itself uses and therefore they imported. They include classes of primitive types, strings, math functions, threads and exceptions. java.util: Language utility classes such as vector, hash tables, random numbers, data etc. java.io: Input/output support classes. They provide facilities for the input and output of data Amity School of Engineering & Technology are automatically

  5. Cont. java.awt: set of classes for implementing graphical user interface. They include classes for windows, buttons, lists, menus and so on. java.net: Classes for networking. They include classes for communicating with local computers as well as with internet servers. java.applet: Classes for creating and implementing applets. Amity School of Engineering & Technology

  6. Amity School of Engineering & Technology Hierarchical representation of java.awt.package java awt Color Graphics java Font Image

  7. Amity School of Engineering & Technology Type of Package Package in java can be categorized in two form, built- in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages.

  8. Amity School of Engineering & Technology Advantage of Java Package Java package is used to categorize the classes and interfaces so that they can be easily maintained. Java package provides access protection. Java package removes naming collision.

  9. Amity School of Engineering & Technology Cont..

  10. Amity School of Engineering & Technology How to Create a Package? Creating a package is a simple task as follows Choose the name of the package Include the package command as the first line of code in your Java Source File. The Source file contains the classes, interfaces, etc you want to include in the package Compile to create the Java packages

  11. Amity School of Engineering & Technology Syntax Package packagename; // Name of package to be created Create Class Package p1; class firstp { publicstaticvoid main(String[] args) { System.out.println( This Is The First Class in Package"); } }

  12. Amity School of Engineering & Technology Compile Save class with firstp.java Compile -> javac firstp.java Command for create a package Java d . Firstp.java //The . operator represents the current working directory. Run file Java p1.firstp

  13. Amity School of Engineering & Technology Compile The Same File Using .. Dots Javac d .. Firstp.java //Here .. indicates the parent directory. In our case file will be saved in parent directory which is C Drive

  14. Amity School of Engineering & Technology Create a Subpackage package p1.p2; class c1{ public void m1() { System.out.println("m1 of c1"); } }

  15. Amity School of Engineering & Technology Compile Javac d .. first.p.java Run Java p1.p2.firstp

  16. Amity School of Engineering & Technology Use of Userdefined Package package letmecalculate; public class division { public int div(int a, int b) { return a/b; } } Compile :- Javac d . division.java

  17. Amity School of Engineering & Technology Cont.. import letmecalculate.*; class arthmatic { public static void main(String args[]) { letmecalculate.division obj = new letmecalculate.division(); System.out.println("Division of two number is-:" + obj.div(10,5)); } } 17

  18. Amity School of Engineering & Technology Insert Multiple classes in a Package Package letmecalculate; Class classname1; Compile Javac d . Classname.java Class classname2; Compile Javac d . Classname.java etc 18

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#