Skip to main content
Engineering LibreTexts

1.10: Java Library Source

  • Page ID
  • Throughout the book, you have used classes from the Java library including System, String, Scanner, Math, Random, and others. You may not have realized that these classes are written in Java. In fact, you can take a look at the source code to see how they work.

    The Java library contains thousands of files, many of which are thousands of lines of code. That’s more than one person could read and understand fully, so please don’t be intimidated!

    Because it’s so large, the library source code is stored in a file named Take a few minutes to locate this file on your machine:

    • On Linux, it’s likely under: /usr/lib/jvm/openjdk-8/
      (You might need to install the openjdk-8-source package.)
    • On OS X, it’s likely under:
    • On Windows, it’s likely under: C:\Program Files\Java\jdk...\

    When you open (or unzip) the file, you will see folders that correspond to Java packages. For example, open the java folder and then open the awt folder. You should now see and, along with the other classes in the java.awt package.

    Open in your editor and skim through the file. It uses language features we haven’t yet discussed, so you probably won’t understand everything. But you can get a sense of what professional Java software looks like by browsing through the library.

    Notice how much of is documentation. Each method is thoroughly commented, including @param, @return, and other Javadoc tags. Javadoc reads these comments and generates documentation in HTML. You can see the results by reading the documentation for the Point class, which you can find by doing a web search for “Java Point”.

    Now take a look at Rectangle’s grow and translate methods. There is more to them than you may have realized, but that doesn’t limit your ability to use these methods in a program.

    To summarize the whole chapter, objects encapsulate data and provide methods to access and modify the data directly. Object-oriented programming makes it possible to hide messy details so that you can more easily use and understand code that other people wrote.