A package in Java is a scope that groups a set of related interfaces and classes. Applications can contain hundreds or thousands of individual classes. Placing classes and interfaces into packages makes it easy to work it.
In this tutorial, we'll explore a range of examples of both java and javax packages. Additionally, we'll delve into the key differences between them and have a better understanding of their functions.
2. java Package
The java package holds the core classes or interfaces in the Java programming language. Most classes required to write basic Java programs are in the java package.
One of the most commonly used java packages is java.lang. It contains the key classes that are essential to the design of the Java programming language itself.
The most important class in this package is the Object class, which is the foundation of the class hierarchy. Also, we have the Math class which provides mathematical functions like cosine, sine, square root, etc.
Additionally, another example of a java package is java.net. The package contains classes for developing networking applications. An example class in this package is Inet4Address which represents an Internet Protocol version 4 (IPv4) address.
Furthermore, java.awt (Abstract Window Toolkit) package provides classes and interfaces for developing graphical user interfaces (GUIs) in desktop applications. This package is the foundation for the javax.swing package. TextField is an example class. It's a GUI component that provides an area for user input.
Finally, another common example is java.io. It's a set of classes and interfaces that provide functionality for input/output (I/O) operations in Java. Also, it helps to handle data from diverse sources, such as files, user input/output, etc. One example class from java.io package is FileInputStream. The class is essential to read any type of file including image, text, and binary files.
3. javax Package
The javax package contains classes or interface that extend the functionality of java packages. It's also known as an extension package.
It provides additional functions to some classes already in the core java package. There are several javax packages, but we'll review a few examples.
One of the most common javax packages is javax.swing. It provides a set of classes for creating graphical user interfaces (GUIs) in Java. It's built on top of the Abstract Window Toolkit (AWT). Some of the key classes in this package are JPanel, JFrame, JComponent, JButton, and JLabel. It helps us create GUI components, customize their appearance and add event listeners.
Another example is javax.tool. This package contains interfaces and classes which can be invoked from a Java program. A common interface is JavaCompiler, which helps to invoke Java compiler from programs. Also, we have StandardJavaFileManager, which is based on java.io.File. It helps to manage files.
Additionally, javax.net is another javax package. This package provides classes for network communication using the Java Secure Socket Extension (JSSE) framework. Some of its classes help secure socket communication.
Furthermore, javax.servlet is another popular example. This package provides a set of classes and interfaces for developing web applications that run within a web server. It contains classes that can handle requests and responses. An example class is ServletInputStream, which helps to read binary data from the client's request.
javax.servlet package is commonly used in developing enterprise applications in Java. On September 2017, Oracle announced its intention to submit Java Enterprise Edition (Java EE) to Eclipse Foundation. This transition changed the namespace of this package from javax.servlet to jakarta.servlet.
Finally, javax.crypto provides a set of classes and interfaces for cryptographic processes. An example class is Cipher. The Cipher provides functionality for encryption and decryption. It's the core of the Java Cryptographic Extension (JCE) framework.
4. Comparison Between java and javax Package
javax and java package both provide classes and interfaces to write effective Java programs.
java package contains the core Java Application Programming Interfaces (APIs). It helps to bootstrap any Java program and serves as the foundation for most Java APIs.
On the other hand, the javax package is an extension of the core java package. It provides additional classes built on top of the java package to add advanced features and functionalities.
As Java evolves, new features that require modifications to the core java package are introduced as an extension package. Java is a language that's backward compatible. Introducing new features as an extension makes old programs run.
In this article, we saw different examples of java and javax packages. We deep-dived into examples of the packages and looked at their uses case.
We learned that the javax package is an extension of the core java package. Both packages can be imported smoothly into any Java program.
res – REST with Spring (eBook) (everywhere)