Packages in Java

Raju Pal

In Java, packages are the collection of similar type of classes, interfaces, enumeration and annotation types. These are organized in a well defined manner for ease of use. Packages are similar to header files in C/C++. The main objectives of having a package type structure in Java are as follows:

  1. Code reuse
  2. Organize classes and interface and prevent from name collisions
  3. Separating modules and projects along with application layers (business, web, windows)

Java has seven predefined packages which are as follows:

  1. java.lang (Java language package)- All functionality of this package is by default included in the any class of Java. The popular classes of this package are as follows:
    • String class – All types of inputs and outputs in java are by default of type string. Hence Strings are objects in java
    • Object class – It is the super class of all the classes in java. Hence java follows a single rooted hierarchy of inheritance. The most popular methods of Object class are toString() method and clone() method.
    • System class – System class contains three main class fields as standard input, standard output and error output streams. These fields are references of PrintStream and InputStream classes. We generally use the System class for printing the output  to console or taking the input from user. Now let’s understand the concept of print statement of java.
      To print any string on console, generally we write
      System.out.pritln("Hello, Welcome to StudyKorner");
      Here, we use the println method which is defined in PrintStream class, and to call this method we require object of PrintStream class i.e. out. This object variable is a member of static class System. Hence, we use class name to access this member without creating the object of System class i.e. System.out
  2. java.util (Java utility package)This package contains various collection frameworks (similar to templates in C++) like List,Queue,ArrayList,HashMap etc. and date and time facilities along with various utility classes such as random number generator(Random), StringTokenizer, and bit arrays etc.  The most poplar class of this package is Scanner class which is used to take input from user. We cover the Scanner class in detail in subsequent posts.
  3. java.net (Java networking package)- This package is a collection of classes which are used for networking applications like Socket programming, URLs processing etc. The programs can be executed on different machine by the use of this package. It also supports distributed computing.
  4. java.io (Java input-output package)- This package is used to provide system input and output through data streams and file systems. File handling in java is done by predefined methods and classes of File Reader and writer or File input stream and File output streams. There are many other different classes for different operations of file handling. One of most popular class of this package is BufferedReader, which is used for taking the input from console. 
  5. java.awt (Java abstract window toolkit)-  This package generally used to create the GUI and window based user interfaces and different types of graphics and images. The most important class of this package is Component and Container class. Container is nothing b`ut a type of component  classes
  6. java.appletThis package is only used in applet application development. Applets are used to develop standalone window based applications.
  7. java.sql (Java structured query language)-  This packages mainly deals with Java Data base connectivity (JDBC) and provide various application programming interfaces (APIs) to process and access the data stored in any DBMS (database management system).

The detailed description and explanation of popular classes along with their methods would be described in provided in future posts.

 

 

Thank you !

What do you think about the article?