The Java Class Library
The first part of this book is devoted to the building blocks of the Java language, including statements, expressions, and operators, and the components of object-oriented programming (OOP), such as methods, constructors, classes, and interfaces.
The second part covers how to build things with those blocks by using the Java Class Library. A lot of your work as a programmer is done for you, provided that you know where to look.
The Java Class Library contains more than 4,400 classes. Many of them will be useful in programs that you create.
Oracle offers comprehensive documentation for the Java Class Library on the Web. A page from this documentation is shown in Figure 9.9.
FIGURE 9.9 The Java Class Library’s online documentation.
The home page is divided into sections. The largest section lists all the packages that compose the library, with a description of each. A package’s name describes its purpose, such as the java.io package of classes for input and output from files, Internet servers, and other data sources; and java.time for time and date classes.
On the home page, the largest section presents a list of packages and a short description of each one. Click the name of a package to load a page listing all of its classes.
Each class in the library has its own page of documentation. To get a taste of how to use this reference, follow these steps:
In a web browser, load the page https://docs.oracle.com/en/java/javase/12/docs/api.
Click the java.base link.
Click the link for the java.lang package. That package’s page opens.
Scroll down to the link for the Math class and click it. The page for the class opens.
Find the random() method link and click it. The page jumps to that section.
The Math class page describes the purpose and package for this class. Use a class page to learn how to create an object of the class and what variables and methods it contains.
The Math class has handy methods that extend Java’s math capabilities and turn up often in Java applications. One is random(), a method that produces a random double value from 0.0 to 1.0. Here’s a statement that uses this method:
double d100 = Math.random() * 100;
The random() method produces a randomly generated number ranging from 0.0 up to 1.0, but not including that maximum value. This is a floating-point number, so it needs to be stored in a float or double.
Because this random number is multiplied by 100, the number will be anything from 0 to 100 (not including 100).
This statement rounds the number down to the nearest integer and adds 1:
d100 = Math.floor(roll) + 1;
This statement uses another method of the Math class, floor(), which rounds a floating-point number down to the closest lower integer. A value of 47.52 would be rounded down to 47. Adding 1 makes the value of d100 48.
Without the Math class, you’d have to create your own class to produce random numbers, which is a highly complex task.
Poking around the Java Class Library documentation is a good way to find classes that will save you an enormous amount of time.
Because you’re new to Java, you likely will find some of the documentation difficult to understand as it’s written for experienced programmers. But as you read this book and encounter interesting Java classes, use this reference to find out more about them. A good place to begin is to look up the methods in a class, each of which performs a job, and see what arguments they take and what values they return.
While you are learning about Swing user interface components and classes in the next five lessons, check out their pages in the official documentation. They have more cool methods than this book has time to cover.