Tuesday, June 7, 2022

Overview of the Java base module

When we look at the various modules provided in the Java SE platform, the two largest byfar are the base module and the desktop module. The base module contains a vast array of functionality which that provides a foundation for everything to do with the Java platform. The desktop module by itself functions as a means of dealing with the entire class of user interface libraries provided in Java SE.

As a library with vast functionality, it would be nice if we could establish some kind of classification for it like we did with the Java desktop module. To do this, lets look back at the earliest versions of Java. They started out with four main base libraries: lang, io, net, and util. All other libraries grew out of these.
  • Language features
  • I/O
  • Networking
  • Utilities
The Java desktop module was classified by how it dealt with a variety of different pieces of hardware devices like graphics displays, printers, sound cards, input devices, etc. The base module isn't that different, except the kind of hardware it deals with is more internal and data processing focused. The kinds of hardware dealt with by the base module include hard disks and networking devices.

The Java I/O packages are necessary in order to deal with hard disks. Given some piece of data stored in RAM on a computer, it needs to go through a separate serialisation process before it can be stored on a device. The functionality provided by Java SE deals with this. Networking is similar to I/O except it deals with communication with external computers.

Like with I/O there should be specialized hardware for dealing with networking in a given machine like a network interface controller. The Java SE base module allows you to take advantage of these hardware configurations as well. In those cases that networking is not available, then these functions cannot be used so it might be good to check for networking capabilities before you do anything.

While the networking and I/O packages let you deal with fundamental hardware devices the lang package and its extensions deals with all the core aspects of the language itself. It is in turn an outgrowth of the JVM, and some aspects of it are even integrated into the JVM itself. For example, the JVM has a concept of primitive types. The lang package provides boxed alternatives that are stored as Object references instead of primitives. The JVM also supports loading string constants onto the stack with ldc, and these are objects of the lang package.

The language features of the Java base module are very close to the core concepts of the Java virtual machine and its fundamental architecture. As mentioned earlier, some of its features are indispensible. The utility package can be seen to be anything that is distinguished from the language package simply by what it is not. Utilities are any features not tied to the core of the language.

The utilities provided by the Java base module have certainly undergone significant expansion. They now deal with a wide variety of areas like compression, concurrency, functional interfaces, random number generation, regular expressions, and so on. The Date class of the utility package grew into the many classes of java.time and the StringTokenizer class grew into the java.text package with all its attendant features. Finally, there are many security libraries now which can be considered to be utilities.

Core language features:

The java.lang package is fundamental to the JVM itself. For example, the Object class is the base of the class hierarchy in any Java program and Strings which are basic to the JVM are implemented here for the first time. Further, the Class class is the basis of the entire reflection system. The ClassLoader lets you dynamically load classes at runtime. Runtime deals with the runtime itself. All these things are basic to the Java virtual machine.

In order to support non-trivial reflection it should be the case that the Class class should allow you to get its fields, methods, constructors, etc and any data associated to them. The second language package therefore that we look to is java.lang.reflect. This is the basis of the Java reflection API.

A long standing issue with Java is interaction with the garbage collector. The java.lang.ref package was introduced to deal with that. As the language evolved, so to did the language packages. The java.lang.annotation package was introduced to handle annotations which were introduced in Java 5.

The language packages follow the evolution of the Java language and the virtual machine. The introduction of new invokation routines to the JVM like invokedynamic necessitated the introduction of the java.lang.invoke package with all of its attendant features. Invokedynamic, one of the more complicated features of the JVM, allows for JVM level support for dynamic languages and features. The features in the language packages are most closly tied to the JVM.

I/O

The basic idea of the I/O package was to enable developers to handle reading and writing to byte streams. With the read and write methods of InputStreams and OutputStreams you can read and write data to and from the hard disk. Further, with the Serializable interface Java objects can be converted to and from objects on a storage device. Readers and writers extend the input and output streams to handle I/O on the level of characters instead of bytes.

The limitation fo the I/O package were resolved with the introduction of the new I/O system in the java.nio package. The Java NIO API introduces high-performance networking and file handling to the JVM. It is distinguished by its use of non-blocking I/O operations and a buffer oriented approach. The Files class also provides a number of utilities that make Java I/O a lot easier to handle.

Networking

The Java platform, as the number one developer platform in the world, should be able to handle all sorts of hardware devices. In the case of networking, the Java SE platform was designed from the very start to be able to handle networking. A basic idea has always been that JVM code, even untrusted, could be communicated and run over the network.

So the Java networking package provides all the functionality you need to deal with URLs, UDP sockets, TCP sockets, IP addresses, and more. Extensions also provide support for the secure socket package. So there is certainly a lot to cover here when dealing with the Java networking APIs.

Utilities

Finally we have a wide variety of different utilities in the base module. These span a vast area of different areas extending the basic functionality of the language package. For example, the collections framework is provided by java.util so that you can have all the fundamental types of collections available to you for work with the Java virtual machine.

The introduction of lambdas in Java led to a significant change in the API. We now have functional interfaces that let you work with various programs that use the new language features. In particular, the stream API extends the collections framework so you can handle Java collections functionally. On top of that there are concurrency and compression APIs. All this explains why the Java base module is so vast.

Disclaimer
These icons are part of the Oxygen Icon Set. All rights to these respect icons belong to their respective owners. They are included here for educative purposes.

No comments:

Post a Comment