X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Packages :

A package is a collection of classes, interfaces and sub-packages. A sub-package in turns divides into classes, interfaces, sub-sub-packages, etc.

Learning about Java is nothing but learning about various packages. By default one predefined package is imported for each and every Java program and whose name is java.lang package.

Whenever we develop any Java program, it may contain many number of user defined classes and user defined interfaces. If we are not using any package name to place user defined classes and interfaces, JVM will assume its own package called NONAME package.

NONAME package will exist only for a limited span of time until the program is completing its purpose.


Advantages / benefits of packages :

If we develop any application by using the concept of packages then we will get the following advantages :-

  • Application development time reduces.

  • Application occupy less memory space.

  • Application execution time reduces.

  • Application performance is enhanced.

  • Redundancy of code is reduces.

In Java we have three types of packages. They are predefined or built-in or core packages, user or secondary or custom defined packages and third party packages.


X

Sun Microsystem :

Sun Microsystems, Inc. was a company that sold computers, computer components, computer software, and information technology services and that created the Java programming language, Solaris Unix and the Network File System.

On January 27, 2010, Sun was acquired by Oracle Corporation for US $7.4 billion, based on an agreement signed on April 20, 2009. The following month, Sun Microsystems, Inc. was merged with Oracle USA, Inc. to become Oracle America, Inc.

X

persistency :

It refers to the continued or prolonged existence of something.

Predefined Packages:

Predefined packages are those which are developed by Sun Microsystem and supplied as a part of JDK (Java Development Kit) to simplify the task of Java programmer.


Note:

Core packages of Java starts with java. (For example: java.lang.*) and advanced packages of Java starts with javax. (For example: javax.swing.*)


Types of predefined packages:

Predefined packages are classified into three types :-

  • JSE packages (Core packages)




  • JEE packages (Advanced packages)




  • JME packages (Micro/Mobile packages)




JSE packages are used for developing client side applications. All the JSE packages start with java.package_name.*;

JEE packages are used for developing server side applications. All the JEE packages start with javax.package_name.*;

JME packages are used for developing mobile applications. All the JME packages start with javax.package_name.*;

As a part of JSE we have following predefined packages :


java.lang.*

This package is used for achieving the language functionalities such as conversion of data from String to fundamental data, displaying the result on the console, obtaining the garbage collector facility. This package is by default imported for each and every Java program that's why it is also called default package.


java.io.*

This package is used for developing file handling applications, such as, opening the file in read or write mode, reading or writing the data, etc. to achieve data persistency.


java.awt.* (abstract window toolkit)

This package is used for developing GUI(Graphical User Interface) components such as buttons, check boxes, scroll boxes, etc.

Creating a GUI component is nothing but creating an object of appropriate predefined class.

Creating a component button with the name as : save
 
Button b = new Button("save");

java.awt.event.*

Event is the sub package of awt(abstract window toolkit) package. This package is used for providing the functionality to GUI components, such as, when button is clicked or when check box is checked, when scroll box is adjusted either vertically or horizontally.


java.applet.*

This package is used for developing browser oriented applications. In other words this package is used for developing distributed programs.

To fulfil the concept of applet we have a predefined class called Applet and it is present in a package called java.applet. This package contains only one class and whose fully qualified name is java.applet.Applet .

An applet is a Java program which runs in the context of www or browser and whose results are shareable across the globe.


java.net.* (Network programming / Socket programming)

This package is used for developing client server applications.


java.util.* (Collection Framework)

This package is used for developing quality or reliable applications in Java or J2EE. This package contains various classes and interfaces which improves the performance of J2ME applications. This package is also known as collection framework (collection framework is the standardized mechanism of grouping of similar or different type of objects into single object. This single object is known as collection object).


java.text.* (Text processing)

This package is used for formatting date, time, numerical manipulations and other miscellaneous information on day to day business operations.


java.lang.reflect.*

Reflect is the sub package of lang package. This package is basically used to study runtime information about the class or interface. Runtime information represents data members of the class or interface, constructors of the class and types of methods of the class or interface.


X

retrieving :

It refers to finding or extracting information.

java.sql.*

This package is used for retrieving the data from database and performing various operations on database.


User Defined Packages:

A user defined package is one which is developed by Java programmers to simplify the task of the Java programmers to keep set of classes, interfaces and sub packages which are commonly used. Any class or interface is commonly used by many Java programmers then that class or interface must be placed in packages.

Syntax: package pack1[.pack2[.pack3……[.packn]]]; Here, package is a keyword which is used for creating user defined packages, pack1 represents upper package and pack2 to packn represents sub packages.

Example:

package p1; // statement-1 package p1.p2; // statement-2 The statements 1 and 2 are called package statements.


Whenever we create user defined package statement as a part of Java program, we must use package statement as a first executable statement.


Steps for developing a package:

  • Choose the appropriate package name, the package name must be a Java valid variable name and we should ensure that the package statement must be first executable statement.

  • Choose the appropriate class name or interface name and whose modifier must be public.

  • The modifier of constructors of a class must be public.

  • The modifier of the methods of class name or interface name must be public.

  • At any point of time we should place either a class or an interface in a package and give the file name as class name or interface name with extension .java

Example:

// Example.java package p1; public class Example { public Example() { System.out.println("DEFAULT CONSTRUCTOR"); } public void show() { System.out.println("SHOW"); } } //IExample.java package p1; public interface IExample { void disp(); }

Syntax for compiling a package: javac –d . filename.java For example: javac –d . Example.java Here, -d is an option or switch which gives an indication to JVM saying that go to Example.java program take the package name and that package name is created as directory automatically provides no errors are present in Example.java. When Example.java is not containing any errors we get Example.class file and it will be copied automatically into current directory which is created recently i.e., p1(package name). The above program cannot be executed since it doesn’t contain any main method.


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.