Package:
- Packages are nothing but all the related classes and interfaces and .class files.
- Packages are the folders which are binding all the related ".class " files together
- Binding: making something available to related functionalities.
- A simple folder would only groups all the related files . But where as packages are also folders which binds all the related files.
- Every package would be a folder. But every folder can not be a package
How to create user defined package:
- By using a keyword "package".
- package package_name.
- package instanceofjavaforus;
- Class Demo{
- public static void main (String args[]) {
- }
- }
The Directory Structure of Packages:
- To make java compiler to crate a package for us and automatically load all corresponding files into package
- package instanceofjavaforus;
- public Class Demo{
- public static void main (String args[]) {
- }
- }
- Save this as Demo.java
- At this time there is no folder with name instanceofjavaforus
- Javac -d . Demo.java.
- -d : creates directory
- instanceofjavaforus -> Demo.java: a folder with name instanceofjavaforus will be created and inside Demo.class will be there.
Creating Sub packages:
- Create a package with a class;
- package pack;
- public Class Demo{
- public static void main (String args[]) {
- }
- }
- C:/> javac -d . Demo.java
- Now pack-> Demo.class folder structure will be created.
- Now create sub package like this.
- package pack.subpack;
- public Class Abc{
- public static void main (String args[]) {
- }
- }
- c:/> javac -d . Abc.java
- pack->subpack -> Demo.class,Abc.class
How to create jar file representing our package:
- By using dos command
- c:/> jar -cvf file_name package_name.
- Ex: jar -cvf instanceofjava.jar pack
Importing a package:
- We can import packages by using "import".
- To import the package
- import<space><package-name><.><* or name of class you want to use from that package
- To import particular class inside a package>.
- import<space><package-name><.><name of class you want to use from that package>
- As per the naming convention: For package names use small case letters.
- import pack.Demo;
- public Class Xyz extends Demo {
- public static void main (String args[]) {
- }
- }
- import pack.*;
- public Class Xyz extends Demo {
- public static void main (String args[]) {
- }
- }
Commonly using Predefined packages in java:
Java.lang:
Provides classes that are fundamental to the design of the Java
programming language. like
- Integer
- boolean
- Class
- Object
- Runtime
- Thread
- System
- Process
- Package
- String
Java.util.*:
This package contains the collections framework classes, legacy collection classes, event model,
date and time facilities, internationalization, and miscellaneous. some of the interfaces and classes present in java.util package are as follows.
- Collection
- Comparator
- Enumeration
- RandomAccess
- List
- Set
- Map
- Iteraot
- EventListener
- Scanner (class)
Java.io.* :
This package provides classes and interfaces for system input and output through data streams,
serialization and the file system.Some of the classes present in java.io package are as follows.
- BufferedInputStream
- BufferedOutputStream
- BufferedReader
- SerializablePermission
- Console
- File
- FileReader
- FilerWriter
- InputStream
- PrintStream
Java.text.* :
This package is used for formatting date and time on day to day business
operations.Some of the classes present in java.text package are as follows
operations.Some of the classes present in java.text package are as follows
- Annotation
- Collater
- Format
- DateFormat
- DecimalFormat
- MessageFormat
- NumberFormat
- Normalizer
- RuleBasedCollator
- ParsePosition
Java.sql.*
This package is used for retrieving the data from data base and performing
various operations on data base. here some of the classes present in java.sql.
various operations on data base. here some of the classes present in java.sql.
- Drivermanger
- Date
- Time
- SqlPermission
- Timestamp
- CallableStatement
- Connection
- PreparedStatement
- ResultSet
- Statement
- SQLData
- SQLInput
- SQLOutput
No comments