Java Documentation Overview

Course Specific Classes

The methods included in these tables are the ones used in this course, and the most commonly useful methods of each class. Links are provided to the complete documentation if you are interested.

FileResource

This class provides methods for accessing a file on your computer. You can create a FileResource in a variety of ways:

  • new FileResource(), opens a dialog box prompting you to select a file on your computer
  • new FileResource("path/to/file.ext"), uses the given String to find a file on your computer or within your BlueJ project
  • new FileResource(existingFile), uses the given File (typically returned by using a DirectoryResource)

For these examples, assume the variable fr has been created for a specific file.

Method name Description Example
.lines() returns an Iterable that provides access to the contents of this opened file one line at a time
for (String line : fr.lines()) {
    // process each line in turn
}
.words() returns an Iterable that provides access to the contents of this opened file one word at a time
for (String word : fr.words()) {
    // process each word in turn
}
.asString() returns the entire contents of this opened file as one String
String contents = fr.asString();

URLResource

This class provides methods for accessing a web page. You can create a URLResource by giving it a complete URL, or web address (note, it must start with either http:// or https://):

  • new URLResource("http://www.something.com/file.ext"), uses the given address to download the referenced file
  • new URLResource("https://www.something.com/file.ext"), uses the given address to download the referenced file

For these examples, assume the variable ur has been created for a specific URL.

Method name Description Example
.lines() returns an Iterable that provides access to the contents of this opened web page one line at a time
for (String line : ur.lines()) {
    // process each line in turn
}
.words() returns an Iterable that provides access to the contents of this opened web page one word at a time
for (String word : ur.words()) {
    // process each word in turn
}
.asString() returns the entire contents of this opened web page as one String
String contents = ur.asString();

DirectoryResource

This class provides a method for choosing one or more files on your computer. You can only create a DirectoryResource with no parameters:

  • new DirectoryResource()

For these examples, assume the variable dr has been created.

Method name Description Example
.selectedFiles() returns an Iterable that provides access to each of the files selected by the user one at a time
for (File f : dr.selectedFiles()) {
    // process each file in turn
}

StorageResource

This class provides methods for storing and accessing a list of strings of any length. Generally you will start by creating an empty StorageResource, then adding string values as you find them in a file or web page:

  • new StorageResource(), creates an empty list
  • new StorageResource(otherList), creates a list that is an exact copy of otherList

For these examples, assume the variable sr has been created.

Method name Description Example
.add(item) adds the given item to the end of the list of strings
sr.add("first!");
sr.add("next ...");
.size() returns the number of strings stored in this list sr.size() is 2 (after the example above)
sr.size() is 0 (immediately after clear() is called)
.data() returns an Iterable that provides access to each string in the list one at a time
for (String item : sr.data()) {
    // process each string in turn
}
.contains(item) returns true only if the given item is in the list sr.contains("first!") is true
sr.contains("last") is false
.clear() removes all strings from this list, making it empty
sr.clear();

Standard Java Classes

The methods included in these tables are the ones used in this course, and the most commonly useful methods of each class. Links are provided to the complete documentation if you are interested.

String

This class provides methods for accessing a sequence of characters of any length. Strings are immutable, which means they cannot be changed after they are created. There are two general ways of creating a String:

  • String s = new String("Colorful");
  • String s = "Colorful";

For these examples, assume the variable s has the value "Colorful"

Method Name Description Example
.equals(other) returns true only if this string has the same characters and in the same order as the other string s.equals("Colorful") is true
s.equals("colorful") is false
.equalsIgnoreCase(other) returns true only if this string has the same characters and in the same order as the other string, regardless of case s.equalsIgnoreCase("Colorluf") is false
s.equalsIgnoreCase("colorful") is true
.length() returns number of characters in this string s.length() is 8
"".length() is 0
.indexOf(str) returns the index within this string of the first occurrence of the given string
note, indices returned start at 0, the first character in the string, and go to s.length()-1, the last character
note, returns -1 if the given string is not in this string
s.indexOf("l") is 2
s.indexOf("ful") is 5
.indexOf(str, startIndex) returns the index within this string of the first occurrence of the given string, starting at startIndex
note, indices returned start at 0, the first character in the string, and go to s.length()-1, the last character
note, returns -1 if the given string is not in this string
s.indexOf("l", 3) is 7
s.indexOf("o", 1) is 1
.substring(startIndex) returns a string with the characters of this string, starting from startIndex and going to the end of this string
note, indices given start at 0, the first character in the string, and go to s.length()-1, the last character
s.substring(1) is "olorful"
s.substring(5) is "ful"
.substring(startIndex, endIndex) returns a string with the characters of this string, starting from startIndex and going up to, but not including, the character at endIndex
note, indices given start at 0, the first character in the string, and go to s.length()-1, the last character
s.substring(1, 2) is "o"
s.substring(1, 4) is "olo"
.toLowerCase() returns a string with the same characters as this string, but with all letters lowercased s.toLowerCase() is "colorful"
.toUpperCase() returns a string with the same characters as this string, but with all letters uppercased s.toUpperCase() is "COLORFUL"
.startsWith(prefix) returns true only if this string starts with given prefix s.startswith("Color") is true
s.startswith("cool") is false
.endsWith(suffix) returns true only if this string ends with given suffix s.endswith("ful") is true
s.endswith("fool") is false
.split(String regex) returns an array of substrings of the original string
any string matching the given regular expression counts as a separator
note, the answer array does not include the separators

String[] output = s.split("o");
output is now ["C", "l", "rful"]

StringBuilder

Strings are sequences of characters that cannot be changed once they are created. StringBuilder, on the other hand, allows adding and deleting from the sequence dynamically. Just like Strings, each character in the sequence has an index number starting at 0. There are two general ways of creating a new StringBuilder:

  • new StringBuilder(), creates a StringBuilder with no characters in it.
  • new StringBuilder("Colorful"), creates a StringBuilder initialized with the given characters.

For these examples, assume the variable sb is a StringBuilder with the value "Colorful"

Method Name Description Example
.append(char c) adds the given character to the end of the sequence sb.append('X');
sb
now has the sequence "ColorfulX"
.insert(int offset, char c) inserts the given character at the specified index in the sequence
remember that indexing of letters starts at 0
sb.insert(1,'X');
sb
now has the sequence "CXolorful"
.charAt(int index) returns the character at the specified index in the sequence sb.charAt(0) is 'C'
.setCharAt(int index, char c) replaces the character at the specified index in the sequence with the given character sb.setCharAt(1,'X');
sb
now has the sequence "CXlorful"
.toString() returns a String version of the sequence sb.toString() is "Colorful"

Character

Character is a class that provides basic utility functions for computing with characters (letters, digits, symbols, and so forth). It is a class with static methods, meaning you do not have to create a new object to use the class' functionality.

Method Name Description Example
.toLowerCase(char c) returns the lower case version of the given character, if the given character is a letter Character.toLowerCase('A') is 'a'
.toUpperCase(char c) returns the upper case, or capital, version of the given character, if the given character is a letter Character.toUpperCase('a') is 'A'
.isLowerCase(char c) returns whether or not the given character is a lower case letter Character.isLowerCase('A') is false
.isUpperCase(char c) returns whether or not the given character is an upper case, or capital letter Character.isUpperCase('A') is true
.isDigit(char c) returns whether or not the given character is a digit, 0-9 Character.isDigit('4') is true
.isLetter(char c) returns whether or not the given character is a letter, regardless of case Character.isLetter('4') is false

Random

This class provides methods for generating random numbers. To generate more than one random number, only one Random object needs to be created.

  • new Random(), creates a new object for generating random numbers.

For these examples, assume the variable rand is a Random object.

Method Name Description Example
.nextInt() returns a random integer in the full range of Java integers int x = rand.nextInt();
.nextInt(int bound) returns a random integer in the range of 0 to bound-1, inclusive

int x = rand.nextInt(10);
x is now a random single digit, 0-9.

Standard Java Collections

Array

The array is the most basic Java collection. As such, it has limitations in that it has a fixed size and few methods. All elements of an array must have the same type. Like Strings, elements in an array are numbered beginning at 0, and these numbers are referred to as indices.

There are two ways to create an array:

  • String[] words = new String[4]; creates an array that can hold Strings with exactly 4 slots. The index of the first slot is 0, and the index of last slot is 3. When arrays are created in this way, the array is filled with default values for each slot. If this were an array of numbers, that would mean that every slot would be initialized with the value 0. Since this is an array of objects, it means that every slot is initialized with a null value.

  • String[] words = {"this", "is", "an", "array"};creates an array of 4 Strings as well. The difference is that the slots are initialized with the given words.

For these examples, assume the variable words is an array initialized as in the second line.

Method Name Description Example
.length returns the number of slots in the array
note there are no parentheses after the word length (since it is an instance variable, not a method)
words.length is 4
[] gives access to the element in the array at the given index
remember that indices start at 0 and end at length-1
words[0] is "this"
words[1] = "was";
words is now ["this", "was", "an", "array"]

ArrayList

Like arrays, ArrayLists are collections. Unlike arrays, they have more functionality and their size can change. When you create an ArrayList you declare what kind of object it holds using < > notation.

  • ArrayList<String> list = new ArrayList<String>(); creates an empty collection

For these examples, assume the variable list is an ArrayList of Strings and has the words "this", "is", "a", "list" inserted.

Method Name Description Example
.add(object) adds the given object to the end of the collection list.add("yes");
list
is now ["this", "is", "a", "list", "yes"]
.get(int index) returns the object at the given index
remember that indexing starts at 0
list.get(0) is "this"
.set(int index, object) changes the element at the given index to the given object
remember that indexing starts at 0
list.set(1, "was");
list
is now ["this", "was", "a", "list"]
.contains(object) returns whether or not the list has the given object in it list.contains("this") is true
.indexOf(object) returns the index of the given object or -1 if the object is not in the list list.indexOf("this") is 0
.size() returns the number of elements in the list list.size() is 4
.clear() removes all the elements of the list list.clear();
list
is now empty with a .size() of 0
.remove(int index) removes the element at the given index
note that the indices of elements past index get shifted down
list.remove(1);
list is now ["this", "a", "list"]
.remove(object) removes the given element from the list, if it is present list.remove("is");
list
is now ["this", "a", "list"]
Iterable a list is Iterable, allowing access to each item one at a time
for (String s : list) {
    // process each item in turn 
} 

HashSet

A set is a collection that is not intended to have any duplications. Like ArrayLists, the <> notation tells the type of object being stored.

  • HashSet<Integer> set = new HashSet<Integer>(); creates an empty collection

For the following examples, assume the set has the values 101, 202, and 303 in it.

Method Name Description Example
.add(object) adds the element to the set, but only if it is not already present set.add(404);
set.add(101);
set
is now [101, 202, 303, 404]
.contains(object) returns whether or not the set contains the given element set.contains(400) is false
.size() returns the number of elements in the set set.size() is 3
.clear() removes all elements from the set set.clear();
set
is now empty with a .size() of 0
.remove(object) removes the given object from the set, provided it exists set.remove(101);
set
is now [202, 303]
Iterable a set is Iterable, allowing access to each item one at a time
for (Integer i : set) {
    // process each item in turn 
} 

HashMap

A map is a collection that works as a lookup table. The collection is kept as pairs of keys and their values. Like ArrayLists, the < > notation tells the type of object used as keys and the type used as values. For example, suppose we want to keep a collection of names with ID numbers. We can create a HashMap like this:

  • HashMap<String, Integer> map = new HashMap<String, Integer>(); creates an empty collection

For the following examples, assume the map variable has two entries:

  • "Robert" maps to 101
  • "Drew" maps to 252
Method Name Description Example
.containsKey(object) returns whether or not the map has an entry with a key that equals the given object map.containsKey("Robert") is true
.get(object) returns the value associated with the given key, or null if the key doesn't exist map.get("Robert") is 101
.put(key, value) adds a new entry to the map with the given key and value map.put("Susan", 400);
map
now contains 3 entries
.size() returns the number of entries in the map map.size() is 2
.clear() removes all the entries from the map map.clear();
map
is now empty with a .size() of 0
.remove(object) removes the entry with the given key from the map, if it is present map.remove("Robert");
map
now contains 1 entry
.keySet() returns a set of all the keys in the map
for (String s : map.keySet()) {
    // process each key in turn 
} 
.values() returns a collection of all the values in the map
for (Integer v : map.values()) {
    // process each value in turn 
} 

Complete Documentation

Course Specific Classes

Standard Java Classes

Standard Java Collections