Understanding Java Packages List

List of Useful Core Java Packages
List of Useful Core Java Packages

Introduction To Java Packages list

Ain’t finding a book from a library containing thousands of books a tiring task? 

Thousands of books, and you search for the one you are looking for right from the first shelf line by line.

Java_Script
Java Gif

That will be so tiring, Indeed.

Thankfully it’s not so. The university library department is kind enough to group the books Department Wise, followed by Subject Wise categorization, and then books with the same author are placed together. The Department wise categorization is also enlisted as a list outside the library. 

Java_script
Java Concept

The same way Java classes and Interfaces are grouped under a Package. A Package is a collection of similar Java entities such as classes, interfaces, sub-packages, exceptions, errors, and enums. 

To know more about Packages in Java and Java Packages list, read out the entire blog.

Java Packages

As per Official Documentation, A package is a grouping of related types providing access protection and namespace management. Note that types refer to classes, interfaces, enumerations, and annotation types. 

Packages_in_Java

Packages in Java are mainly used for providing access protection and namespace management. 

You can either use built-in packages or create your Package to group related classes and sub-packages together as a programmer. The related classes and interfaces are grouped using Packages. This way, other programmers can have a better understanding of your code.

Why use Packages

Using Packages in Java can be helpful in many ways:-

  • Using packages in Java can aid in Data Encapsulation or Data Hiding.
  • Using packages in Java creates a new namespace so the names of your Class and interfaces won’t conflict with the type names in other packages.
  • You can allow types within the Package to have unrestricted access to one another yet still restrict access for types outside the Package.

Creating Package in Java

The package keyword is used to create Packages in Java. The package statement should be the first line in the source file; there can only be one Package in a source file. The classes, interface, annotations, and enumeration will be placed in the current default package if you don’t specify a package.

blog banner 1

In the below example, a java file called Demo.java is created inside the package code studio.

package codestudio;
class Demo
{
public static void printMe(String str)
{
System.out.println(str);
}
public static void main(String[] args) 
{
System.out.println(“Studying about Java Packages List");
printMe(“Java packages list”);
}
}
The output of the above program is:

Studying about Java Packages ListJava packages list

The above program can be easily compiled if you are using an IDE(Integrated Development Environment). If you are not using an IDE, then you need to follow the following syntax:-

javac -d destination_folder file_name.java

The -d is a switch that tells the compiler where to put the class files. Compiling the Demo.java program using the above syntax.

Java_code

Now on executing the command, a folder with the given package name is created in the specified destination, and the compiled class files will be placed in that folder.

Java_code

Corporates usually use the reverse of the internet domain name as the Package’s name. For example, a company whose internet domain name is codingfreek.com would precede all its package names with com.codingfreek.

With each component of the package name, a subdirectory is associated. So if codingfreek had a tutorial package that contained a Demo.java source file, the series of sub-directories would be as follows:

Java_Package_Code

To better understand the above hierarchical structure of .java files, consider an example:- 

Sun Microsystem has defined a Java package containing classes like System, String, Reader, Writer, Socket, etc. 

These classes represent a particular group or are associated with a particular function e.g. 

  • Reader and Writer classes are for Input/Output operation
  • Socket and ServerSocket classes are for networking etc.

So, Sun has subcategorized the java package into sub-packages such as lang, net, io, etc., and put the Input/Output related classes in the io package, Server and ServerSocket classes in net packages, and so on.

Exploring in-built Java Packages

So you now know how to create a user-defined package, how to compile files inside packages and the hierarchy of packages.

Some packages are already defined in Java and are included in java software. These packages are called built-in packages. These packages contain a large number of classes and interfaces that are useful for various requirements. These packages come automatically with JDK/JRE download in the form of jar files. 

You can view all the in-built packages by unzipping the rt.jar available in the lib folder of JRE, inside the Java directory.

Java_Packages_List
  (Java Packages List)

java.util Package

The java.util package contains the collection framework, legacy collection classes, event model, date and time facilities, and miscellaneous utility classes like string tokenizer, a random-number generator, and a bit array.

Java directly depends on several classes in this package

  1. The HashTable class for implementing hashtables
  2. Enumeration interface for iterating through a collection of elements
  3. StringTokenizer class for parsing strings into distinct tokens
  4. The Collection Framework contains inbuilt implementations of various data structures and algorithms.

Consider the below example of using the ArrayList class of the Collection Framework inside the java.util Package and Calendar class.

import java.util.ArrayList;
import java.util.Calendar;
public class Demo
{
public static void main(String[] args) 
Calendar c = Calendar.getInstance();
// Demonstration  of Calendar's get()method

System.out.println("Current Calendar's Year: " + c.get(Calendar.YEAR));        
System.out.println("Current Calendar's Day: " + c.get(Calendar.DATE));           

// Demonstration of ArrayList class        

ArrayList<String> list = new ArrayList<String>();        
list.add("Java Packages List");        
list.add("Coding Ninjas");        
System.out.println(list);   
 }
}
The output of the above program is:

Current Calendar's Year: 2021Current Calendar's Day: 11[Java Packages List, Coding Ninjas]

There are a lot of useful classes, methods, and interfaces in this package. Do check out the official documentation for more details.

java_util_Package

There are a lot of useful classes, methods, and interfaces in this package. Do check out the official documentation for more details.

java.lang package

Among the in-built Java Packages, the java.lang package provides classes that are fundamental to the design of the Java programming language. The most important Class in this Package is the Object class, which is the root of the class hierarchy and Class whose instances represent the classes at run time.

Furthermore, wrapper classes Boolean, Character, Integer, Float, and Double are also present in this Package.

This Package is automatically imported to each of the Java programs. Some of the essential Java classes and interfaces present in the Java.lang package are listed below:-

ClassDescription
ObjectClass Object is the root of the class hierarchy, every Class has Object as a superclass.
ThreadA thread is a thread of execution in a program. Thread class controls each thread in a multithreaded program.
ThrowableThe Throwable class is the superclass of all errors and exceptions in Java language,
MathThe class Math contains methods for basic mathematical operations in Java

There are a lot of classes and interfaces in the Java.lang package. Do check out the official documentation for more details.

Consider the below example that uses the methods defined inside Math and Integer classes of java.lang package

public class Demo 
{
public static void main(String[] args)
{
System.out.println(“Exploring Java packages list”); 
int a = 60, b = 80;  // Using methods defined inside Math class of Java
int sum = Math.addExact(a, b);
int multiply = Math.multiplyExact(a, b);
System.out.println("Sum is: " + sum + " and multiplication is: " + multiply);
// Using methods defined inside Integer Wrapper class of Java

int c = 100;  // Use to convert an integer to string

System.out.println("Using toString(c): " + Integer.toString(c));
// Use to convert an integer to Binary String

System.out.println("Using toBinaryString(c): " + Integer.toBinaryString(c));  
}
}
The output of the above program is:-

Exploring Java Packages ListSum is: 140 and multiplication is: 4800Using toString(c): 100Using toBinaryString(c): 1100100

java.io Package

This Package is one of the fundamental packages in the Java packages list. It provides classes and interfaces for handling input and output operations using data streams, serialization, and the file system. In Java, all the fundamental input and output operations are based on streams. A stream is a conceptually endless stream of data from InputStream or a Reader to OutputStream of a Writer.

Some of the essential Java classes and interfaces present in the java.io Package are listed below.

ClassesDescription
FileReaderThis Class is used to read character files, the constructors of this Class assume default encoding and default byte-buffer-size are appropriate
FileWriterThis Class is used for writing streams of characters. This class also provides a method to write strings directly.
BufferedWriterThis Class inherits the Writer class and is used to provide buffering for Writer Instances.
BufferedReaderThis class is used for reading text from a character-based input stream and inherits the Reader class,
Java_documenttation
  Source: Documentation
java_Package
Exception classes of the java.io Package, Source Documentation

Consider the following program that uses FileOutputStream class of the java.io package to write a primitive value and a string to a file at the location specified. If the file is not present in the specified location, a new file will be created.

import java.io.FileOutputStream;
public class Demo
{
public static void main(String[] args) 
{
System.out.println("Exploring Java Packages List");
try
{

FileOutputStream fout = new FileOutputStream("/workspaces/Demo-Web/Java/newfile.txt");
// writing specified byte to the output stream

fout.write(65);                                          // Writing String
String str = “Coding Ninjas”;                   // Converting string to byte array
byte[] arr = str.getBytes();
fout.write(arr);                                         // Closing the FileOutputStream object
fout.close();
System.out.println("Successfully added ....");
}
catch(Exception e)
{
System.out.println("Exception Occured");
System.out.println(e);
}
 }
}

The output of the above program is:

Exploring Java Packages ListSuccessfully added ….
The file newFile.txt will contain the following content
ACoding Ninjas

For more information regarding methods do check out the official documentation.

java.awt package

Java is a rich language in terms of in-built Java Packages. The java awt or Abstract Window Toolkit package contains classes and interfaces used to develop graphical user interfaces. The root of all AWT components is the Component class

java_awt_Package
Hierarchy of classes in Java.awt package

Some of the essential classes and interfaces in Java.awt Package is enlisted below:

  1. AlphaComposite
  2. Button
  3. Canvas
  4. CardLayout
  5. Container
  6. Dialog

You can study more about all the classes, interfaces, and methods in Java.awt Package in the official documentation.

There are 11 more in-built java packages in the Java package list. They all provide really useful classes and built-in methods to make a programmer’s life easier. 

Frequently Asked Questions

What is the use of packages in Java?

Using Packages in Java is advantageous because it aids in Data Encapsulation or Data hiding. Also, by using Package, name clashes can be avoided as a package creates a new namespace. Above all, using Packages helps in structuring your code well.

How many packages are there in Java?

Broadly, there are two types of packages in Java, namely, built-in packages and user-defined packages.
There are 14 built-in Java packages. The in-built Java Packages list is shown below:-

(Java Packages List)

What is a Java package with an example?

As per Official Documentation, A package is a grouping of related types providing access protection and namespace management. Note that types refer to classes, interfaces, enumerations, and annotation types.
For example, The java.util package defines a number of useful classes primarily collection classes that are useful for working with a group of objects. It contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).

How do Java packages work?

In Java, package name and directory structure are closely related. So if you create a package called employee, you are creating a new directory called employee. Now all the files under the employee directory are a part of the package employee and all the subdirectories are sub-packages of the employee.

Key Takeaways

This blog attempted to give an overview of Packages in Java. Predefined packages in Java, along with their classes and interfaces, were also introduced.

With this done, you can now switch to learn more about the Java programming language

Solidify your understanding of Computer Science Fundamental by reading top-notch quality articles on Code studio. Don’t forget to upgrade yourself by solving various problems available on CodeStudio and read about the interview experiences of scholars in top product-based companies.