r/learnjava • u/tempanon5 • Aug 13 '20
What is a library in Java?
I hear the term of standard libraries a lot in Java. is a library just a collection of packages, which is indirectly a collection of classes. So would that make a library a collection of classes? What makes a library different from a standard library?
Edit: I do see this answer on this link below where they say packages and standard library are the same thing. https://stackoverflow.com/questions/10868825/where-can-i-find-a-list-of-the-java-standard-libraries
But this link here: https://docs.oracle.com/javase/tutorial/java/concepts/package.html tells me that packages and libraries are a different thing. So I guess my question now also is where can I find this standard library, and are there multiple standard libraries or are there just one?
34
u/FrenchFigaro Aug 13 '20 edited Aug 13 '20
Okay. A Library, in the broad general sense, is a collection of classes, providing tools so that you, as a developper, don't have to write these tools.
Some example are:
Joda Time: it's an old library, no longer needed since Java 8. The Java Time API before that was a pain in the ass to use. Joda simplified that and provided some relatively easy to classes.
JXL API: An old API as well. Managing Microsoft Excel document is a pain in the arse. JXL provide tools to do that in a relatively straightforward way. Other libraries provide similar functions, like Aspose, or Apache POI.
Mockito: It's a library that provides tools to create mock objects for testing purposes. Mocking isn't that terribly difficult, but it can be a pain in the arse without such libraries.
Overall using a library allows you to augment your productivity by allowing you to concentrate on actual problems, and not technical ones. They provide abstraction.
The Standard Library, with any programming language, is a library that comes automatically with the compiler and the runtime so that you don't have to bother looking it up. It is generally provided by the compiler/runtime's editor and/or vendor. In Java, and in many other languages, the Standard Library is part of the language's specifications. What this means in Java is that in order to call your homebrew JVM a JVM and your homebrew JDK a JDK, they have to provide the Standard Library. What this mean for you as a developper is that you never have to wonder whether classes from the standard library will be available. If you use the correct version of Java, they will. If you don't, they might not, but that's not your main problem. In Java, the standard library will contain tools such as Strings, Wrappers for your primitives, the Collections API, most standard Exceptions...
Now, on to packages. Packages are just a way to organize classes. In case where you have conflicts in name (like, between the classes
java.util.Listandjava.awt.List), package also allow to identify classes uniquely. Ultimately, packages are just a fancy way to describe a folder arborescence where the compiled classes are stored, and using them well allows to group your classes and interfaces into packages, with varying criterias (usually, by functionality, or by feature)For example, if you work for an organization called Foo, and you're working on a web application called Bar, and your web apps has a module called TOTO, which has some rest controllers, you might want to group them into a package called
com.foo.bar.toto.controller. Or you might not, if your organization prefers to group classes according to some other criteria.As to your question about where to find the standard library: everywhere. If you have a JDK, any JDK, you have access to the Standard Library.
As to your question about whether there are multiple standard libraries: there's just the one, but there are many implementations of it. Since the behaviour of its elements is a part of the java specification, it does not matter(*) which implementation you use
(* It might matter from a performance point of view, because one implementation might be better optimized than another, but between the main implementations, the differences will be minimal for most mainstream uses)