Chapter:Classes - API source code - Exercises

From Juneday education
Jump to: navigation, search

Exercises on the source code for API classes

Locate the file src.zip on your system. It should be located in the Java installation directory, for instance under the JDK1.8XXX directory (the exact location might vary a little from computer system to computer system).

If you are on a Mac, try this:

$ find /Library/Java -name 'src.zip'

If (still on a Mac) you can't find it there, find out where to look by issuing the command /usr/libexec/java_home which should tell you where to look for the file (using find as above).

On Windows you should look in your Java installation directory, typically C:\Program Files\Java\jdk1.8.0\ (note the version number being a part of the folder name - change accordingly).

Copy this zip file to your home directory and some directory under it (we suggest that you start by creating a directory somewhere under your home directory and copy the zip file to that place).

Enter the directory where you put your copy of src.zip. Unzip the zip file (this will create a lot of new files and directories, which is why we strongly urge you to do this in a new directory dedicated for this exercise).

$ unzip src.zip

Let's study the directory structure from this zip file a little. It is the directory tree under the java directory we are most interested in. List the contents of the java directory.

Q1

What are the folder names under the java directory?

Expand using link to the right to see a suggested answer/solution.

The folder names under the java directory are applet beans lang net rmi sql time awt io math nio security text util

Q2

Do you recognize any of the folder names from package names? Change directory to the relative path java/lang/. List the file String.java:

$ cd java/lang
$ ls String.java

Note: if you're using jdk10 chances are that the directory java is no longer in the root so you may have to change directory to java.base (e g cd java.base).

Expand using link to the right to see a suggested answer/solution.

You might recognize lang, and util which are part of the packages java.lang and java.util. Remember, package names are relative paths, so the package java.lang corresponds to the path java/lang and the package java.util corresponds to the path java/util and the paths really exists also for the compiled class files used in runtime (class files are organized in the java installation in the file rt.jar in the exact same directory structure as in the zip file used here with the source code files).

Q3

Using less to view java/lang/String.java

Does the file java/lang/String.java exist?

Expand using link to the right to see a suggested answer/solution.

Yes, it exists!

Let's look inside the file. You may open String.java in an editor, or if you think it is easier to stay in the terminal, you can use a pager, such as less (to move around a file in less, use the arrow keys, to quit you press "q").

Q4

What is the package declaration in the source code for String?

Expand using link to the right to see a suggested answer/solution.

package java.lang;

Q5

How many methods called toUpperCase can you find in the source code for String?

Expand using link to the right to see a suggested answer/solution.

2 methods exist with the name toUpperCase

Q6

Locate the definition of the method toUpperCase() (with no arguments). What is the return type of that method?

Expand using link to the right to see a suggested answer/solution.

The return type of the toUpperCase() method is String

Q7

Go to the online API documentation for String (use a search engine if you don't know the address by heart). How many methods called toUpperCase does the documentation list?

Hint: For Java 7, the online API is located at this page and for Java 8, it is located at this page.

A great way to quickly find the online API documentation for a class, is to use a search engine to find it. A good search phrase for the documentation of String would be java.lang.string se8 for the Java 8 version. Often, you don't need to include the package name, simply search for java ClassName where ClassName could be e.g. arraylist.

Expand using link to the right to see a suggested answer/solution.

The documentation also lists two methods called toUpperCase

Q8

Also in the online API documentation, lookup toUpperCase() (with no arguments). Can you see that the return type is the same as in the source code?

Expand using link to the right to see a suggested answer/solution.

Yes, the return type is listed as String also in the documentation

Q9

Now, look at the source code for java.lang.System. Are there any constructors?

Expand using link to the right to see a suggested answer/solution.

Yes, there is one constructor in java.lang.System, private System(){}

Q10

What is the access modifier for the constructor(s)?

Expand using link to the right to see a suggested answer/solution.

The access modifier of the only constructor in System is private

Q11

Can you create an instance of System?

Hint: If you don't know the answer from looking at the constructors, try to write a small program and to the following in the main method: System sys = new System();

Expand using link to the right to see a suggested answer/solution.

It is not possible to call a private member of a class, and since the only constructor is private, it is not possible to create an instance of java.lang.System!

Q12

Locate the class variable with the name out. What is the full declaration of out?

Expand using link to the right to see a suggested answer/solution.

The full declaration of out in System is: public final static PrintStream out = null;

Q13

Can we use out from another class?

Expand using link to the right to see a suggested answer/solution.

Yes, out is declared public, so we can use it from another class!

Q14

Have you used out before?

Expand using link to the right to see a suggested answer/solution.

Yes, you have used it many times, for printing to the standard out stream like this: System.out.println("hello");, for instance. The variable out is of type "reference to PrintStream" and PrintStream has an instance method called println which you have used many times.

Q15

How would we use out from another class?

Expand using link to the right to see a suggested answer/solution.

out is declared public static, so the syntax from another class would be System.out

Q16

What is the full class name (the qualified name) of the class which out is a reference to?

Expand using link to the right to see a suggested answer/solution.

The out is of type reference to java.io.PrintStream

Q17

How can the System class use the name PrintStream which is defined in another class in another package?

Expand using link to the right to see a suggested answer/solution.

  1. The class System has an import statement saying import java.io.*; which allows System to use the shorter class name PrintStream for java.io.PrintStream

How did we know that it must be the import java.io.*; that allowed them to say only PrintStream instead of the real, longer, name java.io.PrintStream?

Well... A lot of named classes were imported, like:

  • import java.lang.reflect.Executable;
  • import java.lang.annotation.Annotation;
  • import java.security.AccessControlContext;
  • import java.util.Properties;
  • import java.util.PropertyPermission;
  • import java.util.StringTokenizer;
  • import java.util.Map;
  • import java.security.AccessController;
  • import java.security.PrivilegedAction;

etc...

But in that list of explicitly named classes, PrintStream was missing. Hmmmm... So why didn't they have to use the long (full) name java.io.PrintStream when they declared the out variable?

The answer must be that some import statement was used without explicitly naming the PrintStream class!

The answer, thus, must be the only import statement which doesn't explicitly name a class, e.g.: import java.io.*;

So, from this we learn, that importing a "directory" or "package" using the * in place of a Type name, means, "this is also a package, from which we are using a lot of names" .

In fact, if we do:

$ ls java/io/

then, we'd get a list of many types in that package, one of which is PrintStream.java. So the really long answer is:

"Since the java.io.PrintStream class is not explicitly imported, it must be the only import statement left, which was the one that used the *."

Probably, they used the * because, they were using a lot of named types from the java.io package, so it was simply more practical to import all of those names.

Q18

Next, look at the source code for java.lang.Math. Can you create an instance of Math? Why?

Expand using link to the right to see a suggested answer/solution.

No, you cannot create an instance of Math, because the only constructor is private.

Q19

Are there any instance methods in java.lang.Math?

Expand using link to the right to see a suggested answer/solution.

No, there are only static (class) methods in java.lang.math

Q20

How many methods are called max?

Expand using link to the right to see a suggested answer/solution.

There are four methods in java.lang.Math called max

Q21

What are the differences between the various versions of max()?

Expand using link to the right to see a suggested answer/solution.

The parameters differ. Max returns the greater of two numbers, and is defined for comparing:

  • Two int numbers
  • Two long numbers
  • Two float numbers
  • Two double numbers

This is called "overloading" a method. You provide variations of a method with the same name, but different arguments.

Links

Where to go next

Next page is: Classes_-_Other_keywords

« PreviousBook TOCNext »