Chapter:Objects - Java API documentation - Exercises

From Juneday education
Jump to: navigation, search

Exercises on using the Java API online documentation

  1. Navigate to the String documentaion page
    How many constructor does the documentation present? How many have you used?
  2. Still on the page for String, find the length method. What type of value does it return? What does the value represent?
  3. Navigate to the java.util.ArrayList class page.
    How many methods called remove can you find? How do you think Java knows which one you are using if you use one of them?
  4. Still looking at the documentation page for ArrayList read about the remove(int index) method. When an element at an index is removed, what happens to the rest of the elements?


Expand using link to the right to see the full content.

  1. 15 (in the Java version 7 documentation) constructors are listed. You have only used on, the one which takes a String argument (and you have used the double quotes to create Strings - perhaps that should count as a constructor, even if it is unique to String).
  2. It returns an int representing the length of the String (all characters including white space and special characters)
  3. There are two versions of the remove method. Having more than one method with the same name is called overloading (if you like technical words). The java compiler will decide which one you mean based on first the name, then the types of the variables. If you call myList.remove(3) the argument 3 has type int, so the compiler will understand which method you mean by matching the name and type of arguments to the corresponding method in the ArrayList class definition.
  4. When an element at an index is removed, any elements at greater indices (you can think of them as elements to the right of the element to be removed) are shifted one position to the left:
    Before if the list has the following Strings:
    ["I", "am", "Groot"]
    it will become like this if you call remove(1) (remove the element at index 1 - "am")
    ["I", "Groot"]


Exercise help links

Read the Java API documentation for a list of constructors and instance methods and more for the various classes used in the exercises:

Further reading

Where to go next

Next page is: Objects_-_Questions_and_Answers

« PreviousBook TOCNext »