In this article, we will review the following concepts in general terms:
- the Java Library
- packages in Java
In real life, whenever you go to a library, you see a huge number of books and they are divided into different sections: history, mathematics, chemistry, sociology, etc. These books can answer any questions you may have. So there's no need to discover the continents again – you can simply take a book and find what you need.
And all the books are strictly organized:
- on this shelf, you can find algebra books
- on this shelf, geometry books
- on this shelf, books dedicated to mathematical analysis
And you know what? In Java, there’s a similar (virtual) library of tested code, which includes ready-made frameworks for many problems which programmers face in their daily work. That means that you can easily take a code from the library and use it. And this can save a programmer a lot of time, because the code doesn't have to be written from scratch.
The information in the Java library is divided into “packages” – kind of like shelves in a library. And in each package you can a find tested code.
For example, there are such packages:
- java.lang – this is the main package of the Java language
If you are very curious, you can find all the packages on the Oracle website: https://docs.oracle.com/javase/8/docs/api
Let’s take a look at a schematic illustration of the Java Library.
As you can see, various packages are shown on the left side of the picture:
And what's in those packages? They each contain classes. You can see this concept illustrated above in the example of the "java.lang" package. One of the class is java.lang is the "Math" class.
We will full explain the concept of classes in a separate article when we will talk about object-oriented programming (OOP) in Java. But right now, all you need to know about classes is that they are ready-made frameworks which we can use when we write programs.
Each class has its own methods (actions) which can be performed by means of class. For example, the Math class has the following methods:
- sqrt () – the derivation of a square root
- sin () – the calculation of a sine of an angle
- cos () – the calculation of a cosine of an angle
That means that if we need to calculate a square root, we don’t need to write a code that will, step by step, calculate the square root. Such a code would take many lines of code…so, instead, we just take the Math class from the java.lang package (which contains the sqrt () method that calculates the square root) and ta-da: we receive the necessary solution from just one line of code.
We hope that you now understand what the Java Library is, what kind of structure it has, and how it saves programmers' valuable time.
Gradually, you will become acquainted with the different classes and methods in the Java Library, and most importantly, how to use them in your code.