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?
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