r/learnjava • • 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?

56 Upvotes

5 comments sorted by

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.List and java.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)

11

u/zero_as_a_number Aug 13 '20 edited Aug 13 '20

ok.. let's unravel this.

in java you write Code in classes. classes are stored as plaintext files on disk with the .java file name extension.

The more complex a program gets, the more classes you will have. So you may want to start clustering them together. that's where packages come into play. they represent an hierarchical directory structure. a package "com.acme.util" for example would mean that you habe 3 nested directories (packages) on your classpath.

java classes need to declare which package they are in (via the "package" Statement at the very top of the file), with the exception of the "default package" which is rather a virtual package as it just means that your class is present on the top level of your classpath. In our "com.acme.util", the "default package" would be the directory which contains the "com" directory.

The jdk (specifically javac) translates .java files to .class files during the compilation process. You may then choose to further package your class files (e. g. when you want to distribute your Code to use it in another program) into .jar files (which are really just uncompressed zip files in the end)

and that's what ppl commonly call a library: one or more jar files with a specific set of capabilities.

The "Standard library" is what you already use anyway: you get that when you download a JDK.

if you look into a jar file, you will not find any java platform specific classes or libraries bc the assumption is, that those are already present on the target environment - which obviously also needs to have a jre installed to at least be able to run your Code.

So per se, there is only one Standard library for Java, it is what we commonly know as Java 8 / 9 / 11 / 13....


edit : minor fixes due to mobile fuckups

2

u/abcoolynr Aug 13 '20

the standard library is the library(package containing classes) shipped along with a Java version. Whereas the library includes standard+user-defined libraries.

2

u/[deleted] Aug 13 '20

This term is not exclusive to Java. There's a wikipedia page on [Library](https://en.wikipedia.org/wiki/Library_(computing))

Standard library is the collection of classes that comes with JDK and is considered official/public Java API.

1

u/Tomato_Sky Aug 13 '20

A lot of great, clear explanations which is pretty rare in these kinda subs. Take my upvotes everyone!