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

15-Packages-in-Java

The document provides an overview of packages in Java, explaining their purpose, types, and the structure for creating user-defined packages. It details predefined packages such as java.lang, java.sql, java.io, and others, highlighting their functionalities and classes. Additionally, it discusses the compilation and execution of Java programs using packages, along with naming conventions and import statements.

Uploaded by

sunnydwal7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

15-Packages-in-Java

The document provides an overview of packages in Java, explaining their purpose, types, and the structure for creating user-defined packages. It details predefined packages such as java.lang, java.sql, java.io, and others, highlighting their functionalities and classes. Additionally, it discusses the compilation and execution of Java programs using packages, along with naming conventions and import statements.

Uploaded by

sunnydwal7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Fundamentals-Packages in Java

▪ It is an encapsulation mechanism to group Ex : String, StringBuffer, StringBuilder,....


related classes and interfaces into a single unit, Object, System, Class,......
which is nothing but package. Exception , ArithmeticException,
▪ All classes and interfaces which are required for NullPointerException,...
database operations are grouped into a single Thread, Runnable, Cloneable, Comparable......
package which is nothing but java.sql package. Integer, Byte, Short, Float, Long,.....
▪ All classes and interfaces which are useful for Ex-2 : java.io:
file io operation or grouped into a separate ▪ This package is able to provide predefined classes
package which is nothing but java.io package. and interfaces in order to perform Input and
Output operations in Java.
Objectives of Packages :-
▪ This package includes the following classes and
1. To resolve naming conflicts (i.e. unique
interfaces in java applications.
identification of our components).
Ex :
2. It improves modularity of the application.
InputStream, ByteArrayInputStream,
3. It improves maintainability of the
FileInputSTream,.....
application.
OutputStream, ByteArrayOutputStream,
4. It provides security for our components.
FileOutputStream,.....
Types of Packages :- Reader, CharArrayReader, FileReader, .......
There are two types of packages in java: Writer , CharArrayWriter, FileWriter,.......
1) Predefined Packages Serializable, Externalizable,.......
2) User defined Packages Ex-3 : java.util:
1) Predefined Packages :- ▪ This package is able to provide all predefined
▪ These packages are provided by JAVA classes and interfaces which are representing
programming language along with Java software. data structures.
Ex-1 : java.lang: ▪ This package is able to provide classes and
▪ This package is default package in java interfaces like below.
applications, no need to import this package to Ex : Collection, List, Set,Queue
the present java file, when we save java file with ArrayList, Vector, Stack, LinkedList.
.java extension then automatically java.lang HashSet, LinkedHashSet, SortedSet,
package is imported internally. NavigableSet, TreeSet
▪ This package is able to provide all fundamental Queue, PriorityQueue, BlockingQueue,
classes and interfaces which are required to LinkedBlockingQueue,.....
prepare basic java applications. Map, HashMap, LinkedHashMap,
▪ java.lang package includes the following classes IdentityHashMap, WeakHashMap,.....
and interfaces to prepare java applications. Ex-4 : java.awt:

1
▪ This package is able to provide predefined classes JColorChooser, JFileChooser,......
and interfaces representing all GUI components Ex-6 : java.net:
in order to prepare GUI applications. ▪ If we prepare any java application without using
▪ This package has provided the following classes Client-Server Arch then that java application is
and interfaces to prepare GUI applications. called as Standalone Application.
Ex : Component, Label, TextField, ▪ If we prepare any java application on the basis
TextArea, Button, CheckBox, of Client-Server Arch then that java application
List, Choice, Frame,..... is called as Distributed application.
Ex-5 : javax.swing: ▪ To prepare distributed applications, JAVA has
▪ This package is able to provide predefined library provided the following distributed technologies.
to prepare GUI applications. Ex : Socket Programming
FAQ:- What are the differences between AWT RMI [Remote Method Invocation]
[java.awt] and SWING [javax.swing]? CORBA [Common Object Request Broker
1. AWT provided GUI components are platform Arch/Agent]
dependent GUI components. EJBs [Enterprise Java Beans]
SWING provided GUI components are Platform Web Services
Independent GUI Components. ▪ java.net package has provided predefined library
2. AWT provided GUI components are heavy weight to prepare distributed applications by using
GUI components. Socket Programming.
SWING GUI components are light weight GUI Ex : Socket
components. ServerSocket
3. AWT provided GUI Components are basic GUI URL, URI, URN
components. URLConnection
Ex : Ex-7 : java.rmi:
TextField, TextArea, Label, Button,.... ▪ java.rmi package is able to provide predefined
SWING provided GUI components are most library to prepare Distributed applications on the
advanced GUI components. basis RMI.
EX: JColorChooser, JFileChooser, JTable, JTree,.... Ex : Remote

4. AWT provided GUI components are able to reduce RemoteException

application performance. Naming

SWING provided GUI components are able to Ex-8 : java.sql:


▪ The process of interacting with database from
improve application performance.
java application is called as JDBC [Java
Note:
▪ javax.swing package has provided the following Database Connectivity].

predefined classes and interfaces to design GUI ▪ To prepare JDBC applications, java has provided

applications:- predefined library in the form of java.sql package.

JComponent, JTextField, JPasswordField, ▪ java.sql package has provided the following classes
JButton, JCheckBox, JRadioButton, JFrame, and interfaces to prepare JDBC applications.

2
Ex : Driver, DriverManager, Connection, public class Test{
Statement, PreparedStatement, public static void main(String[] args){
CallableStatement, ResultSet, System.out.println(“Package Demo”);
ResultSetMetaData, DatabaseMetaData, .... }
}
2. User defined Packages :-
▪ These packages are defined by the developers as Compilation Command:-
per their application requirements. 1.
Syntax : package package_Name; javac Test.java
▪ If we want to use package declaration statement Note: Generated Test.class file will be placed in
in java applications then we have to use the current working directory.
following two conditions. 2.
a. Package Declaration Statement must be first javac –d . Test.java
statement. Note: Here, .d is used as destination to place
b. Package name must be unique, it must not generated .class files and dot(.) is indicating current
be sharable and it must not be duplicated. working directory.
where package name may be :-
Test.class file will be placed in :-
1) directly a single name
2) sub package names with .(dot) operator
Ex : package p1;
package p1.p2.p3;
▪ If we want to use package declaration statement
in java files then we have to
Note:- Type javac in command prompt and you will
use the following two condition:
find:-
1) Package declaration statement must be the first
statement in java file after the comment section.
2) Package name must be unique, it must not be
sharable and it must not be duplicated.
There is one universally excepted naming conversion
for packages i.e. to use internet domain name in If the corresponding package structure not already
reverse. available than this command itself will create
Ex : corresponding package structure.
As destination instead of dot(.), we can take any
valid directory name.
Ex : javac –d f: Test.java

How to compile package program :-


Ex :
package com.myproject.mycourse;

3
If the specified directory is not already available than 2. import statement (Any Number (including
we will get compile time error. zero))
Ex : javac –d z: Test.java 3. class/ interface/ enum declaration (Any number
Note: If z: not available than we will get CTE saying (zero also))
– directory not found z: All of the following are valid:
How to execute package program:
▪ At the time of execution we have to use fully
qualified name.
Ex : java com.myproject.mycourse.Test Note:- An empty source file is a valid java program
Output:- Package Demo hence the following are valid java source files.
Ex-1 :
Conclusion 1 :-
Just save a blank file as Test.java. It will compile
In any java source file there can be at-most one
successfully, but will not be execute.
package statement i.e. more than one package
Ex-2 : package pack1;
statement is not allowed otherwise we will get
Just save above file as Test.java. It will compile
compile time error.
successfully, but will not be executed.
Ex : package pack1;
Ex-3 : import java.util.*;
package pack2; Just save above file as Test.java. It will compile
public class A{ successfully, but will not be executed.
} Ex-4 : package pack1;
Output:- Compile time error. import java.util.*;
D:\Java>javac A.java Just save above file as Test.java. It will compile
A.java:2: class, interface, or enum expected successfully, but will not be executed.
package pack2; Ex-5 : class Test { }
Conclusion 2 :- Just save above file as Test.java. It will compile
In any java program the first non-comment successfully, but will not be executed.
statement should be package statement (If it is FAQ:- Is It Possible To Provide More Than One
available) otherwise we will get compile time error. Package Declaration Statement Within A Single Java
Ex : import java.util.*; File?
package pack2; ▪ No, it is not possible to provide more than one
public class A{ package declaration statement within a single
} java file, because, package declaration statement
Output:- Compile time error. must be provided as first statement in java files.
D:\Java>javac A.java ▪ To provide package names in java applications,
A. java:2: class, interface, or enum expected JAVA has provided a convention like to include
package pack1; our company domain name in reverse.

Valid java source file structure:- Ex : package p1; // Valid

1. package statement (at-most one/ Only One) package p2; // Invalid

4
package p3; // Invalid java.util.ArrayList
Ex: real time package declaration using naming ▪ A Java program with import statement:
conventions. import java.io.*;
package com.fliqi.icici.transactions.deposit; BufferedReader br = new BufferedReader(new
com.fliqi---> Company Domain name in InputStreamReader( System.in));
reverse. ▪ A Java program without import statement:
icici -----> client/project name java.io.BufferedReader br = new
transactions---> Module name java.io.BufferedReader(new
deposit ------> sub module name java.io.InputStreamReader(System.in));
▪ If we declare classes and interfaces in a package
Application-1 :
and if we want to use these classes and
D:\abc
interfaces in the present java file then we have
Employee.java
to import the respective package to the present
C:\xyz
java file.
com
▪ To import packages to the present java file we
|----fliqi
have to use the following Syntax: -
|----core
▪ import package_Name.*;
|-----Employee.class
▪ It able to import all the classes and interfaces
D:\javaapps\ packages
of the specified package.
Test.java
Ex : import java.util.*;
Test.class
import package_Name.member_Name;
Employee.java :
▪ It able to import only the specified member from
package com.fliqi.core;
the specified package.
public class Employee{
Ex : import java.util.ArrayList;
String eid;
Note: In java files, we are able to provide at most
String ename;
one package declaration statement, but we are able
float esal;
to provide any number of import statements.
String eaddr;
FAQ:- Is It Possible To Use Classes And Interfaces
public Employee(String eid, String
of A Particular Package without Importing the
ename, float esal, String eaddr){
Respective Package?
this.eid=eid;
▪ Yes, it is possible to use classes and interfaces
this.ename=ename;
of a particular package without importing that
this.esal=esal;
package, but by using fully qualified names of
this.eaddr=eaddr;
the respective classes and interfaces.
}
Note: Specifying class name or interface name along
public void getEmpDetails(){
with package name is called as "Fully Qualified System.out.println("Employee Details");
Name". System.out.println("-------------------
Ex : java.io.BufferedReader ----");

5
System.out.println("Employee Id :"+eid);
System.out.println("Employee Name
:"+ename);
System.out.println("Employee Salary
:"+esal);
System.out.println("Employee
Address:"+eaddr);
}
}

On Command Prompt :-
D:\abc>javac -d C:\xyz Employee.java
Test.java
import com.fliqi.core.*;
public class Test{
public static void main(String[] args){
Employee emp=new Employee("E-
111", "Dinesh", 50000.0f, "Gurugram");
emp.getEmpDetails();
}
}

On Command Prompt :-
D:\javaapps\packages>set
classpath=C:\xyz;.;
D:\javaapps\packages>javac Test.java
D:\javaapps\packages>java Test
--- Employee Details----
Classpath : To specify the location where package
is existed.
import com.fliqi.core.* : To specify in which package
Employee class is existed.

~For Revision Work~

You might also like