0% found this document useful (0 votes)
6 views42 pages

M250 Java Reference

The M250 Java Reference booklet serves as a simplified guide to Java classes and interfaces encountered in the M250 module, intended for study and examination use. It includes essential methods and constructors for classes like String and StringBuilder, along with an overview of the Java collections framework. The document is not meant to be read sequentially but rather used as a reference tool for specific information.

Uploaded by

merilif543
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views42 pages

M250 Java Reference

The M250 Java Reference booklet serves as a simplified guide to Java classes and interfaces encountered in the M250 module, intended for study and examination use. It includes essential methods and constructors for classes like String and StringBuilder, along with an overview of the Java collections framework. The document is not meant to be read sequentially but rather used as a reference tool for specific information.

Uploaded by

merilif543
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Unless otherwise stated, copyright © 2026 The Open University, all rights reserved.
Printable page generated Wednesday 7 January 2026 at 12:55

M250 Java Reference

Introduction
This booklet provides a reference to many of the classes and interfaces you have encountered in M250
and is intended for use during your study of the module and in the final exam. It is based on the Java
Application Programming Interface (API) for those classes and interfaces, but differs from the Javadoc in
the following respects:

We have simplified the class and method comments and, in places, the method headings.
We have not mentioned exceptions thrown by some of the methods.

For each class or interface we do not necessarily show every method that is available; we omit some
methods that are not needed for your study of M250. If you require more detail you can access the
full documentation for the Java Class Libraries from BlueJ’s Help menu.
Similarly, we do not always list any or all of the interfaces implemented by a class.

This booklet is not designed to be read from cover to cover; rather, you should use the navigation pane
and search function to find the documentation for a particular method, class or interface.

You are allowed to use this booklet in the examination.

1 Strings
The String and StringBuilder classes have a number of methods that differ only in the type of their
argument. In such cases, we list the method only once and give the argument as T anArgument, which is
M250 shorthand to tell you that there are multiple methods, one for each of the primitive types and one
that takes an argument of type Object. For example:

static String valueOf(T anArgument)

Class String
[Link]
[Link]

public final class String extends Object


implements Comparable<String>

The String class represents character strings. All string literals in Java programs, such as "abc", are
implemented as instances of this class. Instances of the String class are immutable: they cannot be
changed once created.

[Link] 1/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Constructor summary
String()

Initialises a new String object so that it represents an empty character sequence.

String(char[] value)

Initialises a new String object so that it represents the sequence of characters currently contained
in the character array argument.

String(String original)

Initialises a new String object so that it represents the same sequence of characters as the
argument; in other words, the new string is a copy of the argument string.

String(StringBuilder builder)

Initialises a new String object that contains the sequence of characters currently contained in the
StringBuilder argument.

Method summary
In the following methods, where the formal argument is of type CharSequence, you can assume for our
purposes that the actual argument is a String object. If a formal argument is given as int ch, you can
assume for our purposes that the actual argument is of type char.

char charAt(int index)

Returns the char value at the index specified by the argument.

int compareTo(String anotherString)

Compares this string to the argument string character by character, based on the Unicode value of
each character in the strings.

If this String comes before the argument string alphabetically, returns a negative int. If this String
comes after the argument string alphabetically, returns a positive int. (Note that an upper-case letter
comes before the same lower-case letter.)
The size of the returned int (positive or negative) tells you how far apart in the character sequence
the first unequal characters are.
If the two strings are exactly equal, 0 is returned.

int compareToIgnoreCase(String str)

[Link] 2/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Compares this string to the argument string character by character as for the compareTo() method,
but ignoring case differences.

String concat(String str)

Returns this string if the length of the argument string is 0. Otherwise, returns a new String object
that is the concatenation of this string followed by the argument string.

boolean contains(CharSequence s)

Returns true if this string contains the sequence of char values contained in the argument as a
subsequence, false otherwise.

boolean contentEquals(CharSequence cs)

Returns true if this string contains the same sequence of char values as the argument, false
otherwise.

static String copyValueOf(char[] data)

Returns a new String that has the same characters, in the same order, as the char array argument.

boolean endsWith(String suffix)

Returns true if this string ends with the argument, false otherwise.

boolean equals(Object anObject)

Returns true if the argument is not null and is a String object that holds the same sequence of
characters as this string, false otherwise.

boolean equalsIgnoreCase(String anotherString)

As for the equals() method but ignoring case differences.

static String format(String format, Object... args)

Returns a formatted string using the specified format string and arguments. (The notation ‘...’ in an
argument list indicates that a variable number of arguments (0 or more) is allowed.)

int hashCode()

[Link] 3/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Returns the hash code of this string.

int indexOf(int ch)

Returns the index of the first occurrence of the argument ch within this string. If the character does
not occur within this string, -1 is returned.

int indexOf(int ch, int fromIndex)

Returns the index of the first occurrence of the argument ch within this string, starting the search at
fromIndex. If the character does not occur within this string, -1 is returned.

int indexOf(String str)

Returns the index of the first character of the first occurrence of the argument str within this string. If
the string does not occur within this string, -1 is returned.

int indexOf(String str, int fromIndex)

Returns the index of the first character of the first occurrence of the argument str within this string,
starting the search at fromIndex. If the string does not occur within this string, -1 is returned.

boolean isEmpty()

Returns true if this string’s length() is 0, false otherwise.

int length()

Returns the length of this string.

String replace(char oldChar, char newChar)

Returns a copy of this string in which all occurrences of oldChar have been replaced with newChar.

String[] split(String regex)

Returns an array of strings computed by splitting this string around matches of the given regular
expression. Any trailing empty strings are discarded. For example:

"boo:and:foo".split(":");

returns the array {"boo", "and", "foo"}

[Link] 4/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

As another example:
"boo:and:foo".split("o");

returns the array {"b", "", ":and:f"}

boolean startsWith(String prefix)

Returns true if this string starts with the argument, false otherwise.

String substring(int beginIndex)

Returns a substring of this string beginning at beginIndex and continuing to the end of the string.

String substring(int beginIndex, int endIndex)

Returns a substring of this string from beginIndex to endIndex - 1.

char[] toCharArray()

Returns an array of char that has the same characters, in the same order, as this string.

String toLowerCase()

Returns a copy of this string with all the characters in lower case.

String toString()

Returns this string, which is already a string!

String toUpperCase()

Returns a copy of this string with all the characters in upper case.

String trim()

Returns a copy of this string with leading and trailing whitespace omitted.

static String valueOf(T anArgument)

Returns the string representation of the actual argument, which can be of any primitive or reference
type. For example, if x references an object, [Link](x) and [Link]() return the

[Link] 5/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

same string.
Class StringBuilder
[Link]
[Link]

public final class StringBuilder extends Object


implements Appendable

The StringBuilder class represents character strings. However, unlike instances of String, instances
of StringBuilder are mutable, so can be changed once created.

StringBuilder implements the Appendable interface, which means that it has the append(char)
method.

Constructor summary
StringBuilder()

Initialises a new empty StringBuilder object.

StringBuilder(String str)

Initialises a new StringBuilder object so that it represents the same sequence of characters as
the argument.

Method summary
See also the Appendable interface.

StringBuilder append(T anArgument)

Appends a string representation of the actual argument (which can be of any primitive or reference
type) to this object. Returns this StringBuilder.

char charAt(int index)

Returns the char value at the index specified by the argument.

StringBuilder delete(int start, int end)

Removes a substring from this object, from start to end - 1. Returns this object.

StringBuilder deleteCharAt(int index)

[Link] 6/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Removes the char value at the index specified by the argument. Returns this object.

int indexOf(String str)

Returns the index of the first character of the first occurrence of the argument str within this object.
If the string does not occur within this object, -1 is returned.

int indexOf(String str, int fromIndex)

Returns the index of the first character of the first occurrence of the argument str within this object,
starting the search at fromIndex. If the string does not occur within this object, -1 is returned.

StringBuilder insert(int offset, T anArgument)

Inserts a string representation of the second argument (which can be of any primitive or reference
type) into this object at the index specified by offset. Returns this object.

int length()

Returns the length of this object.

StringBuilder replace(int start, int end, String str)

Removes a substring of this object, which is specified as start to end - 1, and then inserts the
String argument at start. Returns this object.

StringBuilder reverse()

Reverses the order of this object’s characters. Returns this object.

void setCharAt(int index, char ch)

Replaces the character at index in this object with ch.

String substring(int start)

Returns the String that is a substring of this object beginning at start.

String substring(int start, int end)

Returns the String that is a substring of this object from start to end - 1.

[Link] 7/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

String toString()

Returns a String representation of this object.

2 Java collections framework


In this section:

E is used to indicate the declared type of a collection’s elements


K is used to indicate the declared type of a map’s keys
V is used to indicate the declared type of a map’s values
T represents a reference type.

We have simplified the documentation in two ways. Where standard Javadoc for collections would show
the declaration of a formal argument for a method as:

void putAll(Map<K, V> aMap)

or

boolean addAll(Collection<E> aCol)

we have simplified it to:

void putAll(Map aMap)

and

boolean addAll(Collection aCol)

Similarly, for return types. We have simplified:

SortedMap<K, V> tailMap(K fromKey)

to:

SortedMap tailMap(K fromKey)

‘Views’ of collections

[Link] 8/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Note that when a method returns a ‘view’ of a collection, it means that the returned collection is not
independent of the original collection. In other words, changes made to a view of a collection are
reflected in the original collection. Changes to the original collection may also affect a view you have
of it.

Similarly, if a collection A is ‘backed’ by another collection B, changes to collection A are seen in


collection B and vice versa.

2.1 Collection interfaces


Interface Collection
[Link]

Superinterfaces: Iterable

Subinterfaces include: List, Queue, Set, SortedSet

Implementing classes include: ArrayList, HashSet, LinkedList, TreeSet

public interface Collection<E> extends Iterable<E>

The root interface in the collection hierarchy, representing a group of objects, known as its elements.
Some collections allow duplicate elements and others do not. Some are ordered and others unordered.
The Java Development Kit provides implementations of more specific subinterfaces such as Set and
List. The Collection interface is typically used to pass collections around and manipulate them where
maximum generality is desired.

Method summary
boolean add(E element)

Adds the argument to this collection. Returns true if the operation was successful, false otherwise.

boolean addAll(Collection aCol)

Adds all the elements in the argument to this collection. Returns true if the operation was
successful, false otherwise.

void clear()

Removes all the elements from this collection (if there are any).

boolean contains(Object obj)

[Link] 9/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Returns true if the argument is an element in this collection, false otherwise.

boolean containsAll(Collection aCol)

Returns true if this collection contains all of the elements in the argument collection, false
otherwise.

boolean equals(Object obj)

Returns true if the argument object is equal to this collection, false otherwise.

int hashCode()

Returns the hash code of this collection.

boolean isEmpty()

Tests whether this collection is empty. Returns true if empty, false otherwise.

boolean remove(Object obj)

Removes one occurrence of the argument from this collection. Returns true if the operation is
successful, false otherwise.

boolean removeAll(Collection aCol)

Removes one occurrence of each of the argument’s elements from this collection. Returns true if
the operation is successful, false otherwise.

boolean retainAll(Collection aCol)

Retains only the elements in this collection that are also contained in the argument. Returns true if
the operation is successful, false otherwise.

int size()

Returns the number of elements in this collection.

Object[] toArray()

Returns an array containing all the elements in this collection.

[Link] 10/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

E[] toArray(E[] anArray)

Returns an array containing all the elements in this collection; if this collection is ordered, the
returned array preserves the original order. The runtime type of the returned array is that of the
argument.
Interface Iterable
[Link]

Subinterfaces include: Collection, List, Queue, Set, SortedSet

Implementing classes include: ArrayList, HashSet, LinkedList, TreeSet

public interface Iterable<T>

Implementing this interface allows a collection class’s instances to be iterated over by a for-each
statement.

Method summary
For more information, see the Iterator interface.

Iterator<T> iterator()

Returns an iterator over a collection of elements of type T.

Interface Iterator
[Link]

Implementing classes include: Scanner

public interface Iterator<E>

Provides methods that allow iterating over a collection of elements of type E.

Method summary
boolean hasNext()

Returns true if the iteration has more elements.

E next()

Returns the next element in the iteration.

[Link] 11/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

void remove()

Optional operation to remove the last element returned by this iterator. (The method may throw an
UnsupportedOperationException if the remove operation is not supported.) Iterator behaviour is
unspecified if the underlying collection is modified – while the iteration is in progress – in any way
other than by calling this method.
Interface List
[Link]

Superinterfaces: Collection, Iterable

Implementing classes include: ArrayList, LinkedList

public interface List<E> extends Collection<E>

An ordered collection providing control over where in the list each element is inserted and retrieved from.

Method summary
See also the Collection and Iterable superinterfaces.

boolean add(E element)

Appends the argument to the end of this list. Returns true if the operation was successful, false
otherwise.

void add(int index, E element)

Inserts the argument element into this list at the position specified by the argument index. Shifts the
element currently at that position (if any) and any subsequent elements to the right (adds one to their
indices).

boolean addAll(Collection aCol)

Appends all the elements in the argument to the end of this list, in the order that they are returned by
the argument’s iterator. Returns true if the operation is successful, false otherwise.

boolean addAll(int index, Collection aCol)

Inserts all the elements in the argument aCol into this list at the position specified by the argument
index. Shifts the element currently at that position (if any) and any subsequent elements to the right
(adds one to their indices). Returns true if the operation is successful, false otherwise.

[Link] 12/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

boolean equals(Object obj)

Compares the argument obj with this list for equality. Returns true if, and only if, the argument is
also a list, both this list and the argument have the same size, and all corresponding pairs of
elements in the two lists are equal; returns false otherwise.

E get(int index)

Returns the element at the position specified by the argument.

int hashCode()

Returns the hash code for this list.

int indexOf(Object obj)

Returns the index in this list of the first occurrence of the argument, or -1 if this list does not contain
this element.

int lastIndexOf(Object obj)

Returns the index in this list of the last occurrence of the argument, or -1 if this list does not contain
this element.

E remove(int index)

Removes and returns the element at the position in the list specified by the argument.

boolean remove(Object obj)

Removes the first occurrence in this list of the argument. Returns true if the operation is successful,
false otherwise.

E set(int index, E element)

Replaces the element at the position specified by the argument index with the argument element
and returns the original element.

int size()

Returns the number of elements in this list.

[Link] 13/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

List subList(int fromIndex, int toIndex)

Returns a view of the portion of this list between the indices specified by the arguments fromIndex
(inclusive) and toIndex (exclusive).

Object[] toArray()

Returns an array containing all the elements in this list in the same order.

E[] toArray(E[] anArray)

Returns an array containing all the elements in this list in the same order. The runtime type of the
returned array is that of the argument.
Interface Set
[Link]

Superinterfaces: Collection, Iterable

Subinterfaces include: SortedSet

Implementing classes include: HashSet, TreeSet

public interface Set<E> extends Collection<E>

A collection that contains no duplicate elements, and at most one null element. As implied by its name, this
interface models the mathematical ‘set’ abstraction.

Method summary
See also the Collection and Iterable superinterfaces.

boolean add(E element)

Adds the argument to this set, unless it is already there. Returns true if the argument is added,
false otherwise.

boolean addAll(Collection aCol)

Adds all the elements in the argument to this set, unless they are already there. Returns true if the
operation is successful, false otherwise.

boolean equals(Object obj)

[Link] 14/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Compares the argument with this set for equality. Returns true if the argument is also a set, the two
sets have the same size, and every element in the argument is also contained in this set; returns
false otherwise.

int hashCode()

Returns the hash code of this set.


Interface SortedSet
[Link]

Superinterfaces: Collection, Iterable, Set

Implementing classes include: TreeSet

public interface SortedSet<E> extends Set<E>

A set that keeps its elements ordered according to their natural ordering. Several additional operations are
provided to take advantage of this ordering.

Method summary
See also the Collection, Set and Iterable superinterfaces.

E first()

Returns the first element currently in this set.

SortedSet headSet(E toElement)

Returns a view of the portion of this set whose elements are strictly less than toElement.

E last()

Returns the last element in this set.

SortedSet subSet(E fromElement, E toElement)

Returns a view of the portion of this set between the elements specified by the arguments
fromElement (inclusive) and toElement (exclusive).

SortedSet tailSet(E fromElement)

[Link] 15/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
Interface Map
[Link]

Subinterfaces include: SortedMap

Implementing classes include: HashMap, TreeMap

public interface Map<K,V>

A collection that maps keys to values. A map cannot contain duplicate keys; each key can map to at most
one value.

Method summary
void clear()

Removes all entries from this map.

boolean containsKey(Object key)

Tests whether a given key is present in this map. Returns true if the key is present, false
otherwise.

boolean containsValue(Object value)

Tests whether the argument is present as a value in this map. Returns true if the argument is
present as a value, false otherwise.

boolean equals(Object obj)

Compares the argument with this map for equality. Returns true if the argument is also a map and
the two maps represent the same key–value pairs.

V get(Object key)

If the argument exists as a key in this map, returns the associated value. Otherwise, returns null.

int hashCode()

Returns the hash code of this map.

[Link] 16/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

boolean isEmpty()

Tests whether this map contains any key–value pairs. Returns true if the map is empty, false
otherwise.

Set keySet()

Returns a view of the keys contained in this map as a set. If a key is removed from the returned set,
the associated key–value pair is removed from the map and vice versa.

V put(K key, V value)

Inserts a key–value pair into this map. If a key–value pair already exists with the same key, the old
value is overwritten by the new value. Returns the previous value for the key, if there was one, null
otherwise.

void putAll(Map aMap)

Copies all the key–value pairs from the argument into this map.

V remove(Object key)

Removes the key–value pair associated with the argument (if the key exists). Returns the previous
value for the key, if there was one, null otherwise.

int size()

Returns the number of key–value pairs in this map.

Collection values()

Returns a view of the values contained in this map as a collection.


Interface SortedMap
[Link]

Superinterfaces: Map

Implementing classes include: TreeMap

public interface SortedMap<K,V> extends Map<K,V>

[Link] 17/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

A map that guarantees that its elements are sorted in ascending key order, according to the natural
ordering of its keys.

Method summary
See also the Map superinterface.

K firstKey ()

Returns the first key in this map.

SortedMap headMap(K toKey)

Returns a view of the portion of this map whose keys are strictly less than toKey.

K lastKey ()

Returns the last (highest) key in this sorted map.

SortedMap subMap(K fromKey, K toKey)

Returns a view of the portion of this sorted map between the keys specified by the arguments
fromKey (inclusive) and toKey (exclusive).

SortedMap tailMap(K fromKey)

Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey.

2.2 Collection implementation classes


Class HashSet
[Link]
[Link]
[Link]
[Link]

public class HashSet<E> extends AbstractSet<E>


implements Collection<E>, Iterable<E>, Set<E>

Implementation of the Set interface, ‘backed’ by a hash table. It makes no guarantees as to the iteration
order of the set.

Constructor summary

[Link] 18/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

HashSet()

Constructs an empty HashSet.

HashSet(Collection aCol)

Constructs a HashSet containing the elements in the argument.

Method summary
See the Set, Collection and Iterable interfaces.
Class TreeSet
[Link]
[Link]
[Link]
[Link]

public class TreeSet<E> extends AbstractSet<E>


implements Collection<E>, Iterable<E>, SortedSet<E>

Implementation of the SortedSet interface. It guarantees that the sorted set is in ascending element
order, according to the natural order of the elements.

Constructor summary
TreeSet()

Constructs an empty TreeSet.

TreeSet(Collection aCol)

Constructs a TreeSet containing the elements in the Collectionargument, sorted according to the
elements’ natural order.

TreeSet(SortedSet aSortedSet)

Constructs a TreeSet containing the elements in the SortedSet argument, sorted according to the
elements’ natural order.

Method summary
See the Collection, Set, SortedSet and Iterable interfaces.

Class HashMap

[Link] 19/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

[Link]
[Link]
[Link]

public class HashMap<K,V> extends AbstractMap<K,V>


implements Map<K,V>

Implementation of the Map interface based on a hash table. Ordering of keys is not supported.

Constructor summary
HashMap()

Constructs an empty HashMap.

HashMap(Map aMap)

Constructs a HashMap containing the same mappings as the argument.

Method summary
See the Map interface.
Class TreeMap
[Link]
[Link]
[Link]

public class TreeMap<K,V> extends AbstractMap<K,V>


implements SortedMap<K,V>

Implementation of the SortedMap interface. It guarantees that the map keys are sorted in ascending key
order, according to the natural order of the keys.

Constructor summary
TreeMap()

Constructs an empty TreeMap.

TreeMap(Map aMap)

Constructs a TreeMap containing the same mappings as the argument, sorted according to the keys’
natural order.

[Link] 20/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

TreeMap(SortedMap aSortedMap)

Constructs a TreeMap containing the same mappings as the argument, sorted according to the same
ordering.

Method summary
See the Map and SortedMap interfaces.
Class ArrayList
[Link]
[Link]
[Link]
[Link]

public class ArrayList<E> extends AbstractList<E>


implements Collection<E>, List<E>, Iterable<E>

Implementation of the List interface.

Constructor summary
ArrayList()

Constructs an empty ArrayList.

ArrayList(Collection aCol)

Constructs an ArrayList containing the elements of the argument, in the order they appear in the
argument.

Method summary
See the List, Collection and Iterable superinterfaces.

2.3 Collection and other utility classes


Class Arrays
[Link]
[Link]

public class Arrays extends Object

A utility class that contains static methods for manipulating arrays (such as sorting and searching). The
methods of this class throw a NullPointerException if the array references provided to them as
arguments are null.

[Link] 21/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Method summary
static List asList(E[] anArray)

Returns a fixed-size list ‘backed’ by the array argument. (Changes to the returned list are reflected in
the array and vice versa.)

static int binarySearch(int[] intArray, int anInt)

Searches the array argument for the value specified by the second argument using the binary search
algorithm. If found, returns the index of the element, otherwise returns (−(insertion point) − 1).

The array being searched must be sorted (as by the sort(int) method, below); if not, the results
are undefined. If the array contains multiple elements with the same specified value, there is no
guarantee which one is found.

There are equivalent methods for the primitive types byte, char, double, float, long and short.

static int binarySearch(Object[] anArray, Object obj)

Searches the array argument for the object specified by the second argument using the binary
search algorithm. If found, returns the index of the element, otherwise returns (−(insertion point) − 1).

The array being searched must be sorted into ascending order according to the natural ordering of its
elements (as by the sort(Object[] method, below); if not, the results are undefined. If the array
contains multiple elements equal to the specified object, there is no guarantee which one is found.

static boolean deepEquals(Object[] anArray1, Object[] anArray2)

Returns true if the two array arguments are deeply equal to one another. Two array references are
considered deeply equal if both are null or they refer to arrays that contain the same number of
elements and all corresponding pairs of elements in the two arrays are deeply equal.

static int deepHashCode(Object[] anArray)

Returns a hash code based on the ‘deep contents’ of the array argument. If the array contains other
arrays as elements, the hash code is based on their contents and so on, ad infinitum.

static String deepToString(Object[] anArray)

Returns a string representation of the ‘deep contents’ of the array argument. If the array contains
other arrays as elements, the string representation contains their contents and so on, ad infinitum.

static boolean equals(int[] anArray, int[] anArray2)

[Link] 22/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Returns true if the two array arguments are equal to one another, false otherwise. There are
equivalent methods for the primitive types boolean, byte, char, double, float, long and short.

static boolean equals(Object[] anArray, Object[] anArray2)

Returns true if the two array arguments are equal to one another, false otherwise.

static void fill(int[] anArray, int val)

Assigns the int argument val to each component of the array argument. There are equivalent
methods for the primitive types boolean, byte, char, double, float, long and short.

static void fill(int[] anArray, int fromIndex, int toIndex, int val)

Assigns the int argument val to the components of a subarray of the array argument. The range of
the subarray is given by the arguments fromIndex (inclusive) and toIndex (exclusive). There are
equivalent methods for the primitive types boolean, byte, char, double, float, long and short.

static void fill(Object[] anArray, int fromIndex, int toIndex, Object val)

Assigns the Object argument val to the components of a subarray of the array argument. The
range of the subarray is given by the arguments fromIndex (inclusive) and toIndex (exclusive).

static void fill(Object[] anArray, Object val)

Assigns the Object argument val to each component of the array argument.

static int hashCode(int[] anArray)

Returns a hash code based on the contents of the array argument. There are equivalent methods for
the primitive types boolean, byte, char, double, float, long and short.

static int hashCode(Object[] anArray)

Returns a hash code based on the contents of the array argument.

static void sort(int[] anArray)

Sorts the argument, an array of integers, into ascending numerical order. There are equivalent
methods for the primitive types byte, char, double, float, long and short.

[Link] 23/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

static void sort(int[] anArray, int fromIndex, int toIndex)

Sorts a subarray of the array argument into ascending numerical order. The subarray is specified by
the arguments fromIndex (inclusive) and toIndex (exclusive). There are equivalent methods for the
primitive types byte, char, double, float, long and short.

static void sort(Object[] anArray)

Sorts the argument, an array of objects, into ascending order, according to the natural ordering of its
elements.

static void sort(Object[] anArray, int fromIndex, int toIndex)

Sorts a subarray of the array argument into ascending order, according to the natural ordering of its
elements. The subarray is specified by the arguments fromIndex (inclusive) and toIndex
(exclusive).

static String toString(int[] anArray)

Returns a string representation of the contents of the array argument. There are equivalent methods
for the primitive types boolean, byte, char, double, float, long and short.

static String toString(Object[] anArray)

Returns a string representation of the contents of the array argument.


Class Collections
[Link]
[Link]

public class Collections extends Object

A utility class that consists exclusively of static methods that operate on, or return, collections. The
methods of this class throw a NullPointerException if the objects provided to them as arguments are
null.

Method summary
static boolean disjoint(Collection col1, Collection col2)

Returns true if the two arguments have no elements in common, false otherwise.

static int frequency(Collection aCol, Object obj)

[Link] 24/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Returns the number of elements in the argument aCol that are equal to the argument obj.

static int indexOfSubList(List source, List subList)

If sublist is a sublist of source, returns the index of the first element of subList within source;
otherwise, returns -1.

static int lastIndexOfSubList(List source, List subList)

If sublist is a sublist of source, returns the index of the last element of subList within source;
otherwise, returns -1.

static E max(Collection coll)

Returns the maximum element in the argument, according to the natural ordering of its elements. All
elements in the collection must implement the Comparable interface.

static E min(Collection coll)

Returns the minimum element in the argument, according to the natural ordering of its elements. All
elements in the collection must implement the Comparable interface.

static void reverse(List list)

Reverses the order of the elements in the argument.

static void rotate(List list, int distance)

Rotates the elements in the list argument by the distance argument.

static void shuffle(List list)

Randomly shuffles the argument’s elements.

static void sort(List list)

Sorts the argument into ascending order, according to the natural ordering of its elements. All
elements in the list must implement the Comparable interface.

static void swap(List list, int i, int j)

[Link] 25/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Swaps the elements of the argument that are at indices i and j.


Class Random
[Link]
[Link]

public class Random extends Object

A utility class used to generate pseudorandom numbers.

Constructor summary
Random()

Constructs a new random number generator.

Random(long seed)

Constructs a new random number generator using a long seed.

Method summary
boolean nextBoolean()

Returns a random boolean value.

double nextDouble()

Returns a random double value.

int nextInt()

Returns a random int value.

int nextInt(int bound)

Returns a random int value between 0 and bound - 1.

3 Files and streams


This section contains interfaces and classes for writing to and reading from files.

3.1 Input- and output-related interfaces


Interface Appendable

[Link] 26/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

[Link]

public interface Appendable

Implementing classes include: StringBuilder, Writer

Implemented by objects to which char sequences and values can be appended.

Method summary
In the following methods, where the formal argument is of type CharSequence, you can assume for our
purposes that the actual argument is a String object.

append(char c)

Appends the specified character to this object.

append(CharSequence csq)

Appends the specified character sequence to this object.

append(CharSequence csq, int start, int end)

Appends the subsequence of the specified character sequence to this object.


Interface AutoCloseable
[Link]

public interface AutoCloseable

Subinterfaces include: Closeable

Implementing classes include: InputStream, OutputStream, Reader, Writer, Scanner

An object that may hold file-related resources until its close() method is called. The subinterface
Closeable specifies the same method, but only resources managed by try-with-resources statements are
auto-closed.

Method summary
void close() throws Exception

Closes this resource, relinquishing any underlying resources. This method is invoked automatically
on objects managed by a try-with-resources statement.

[Link] 27/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Interface Readable
[Link]

public interface Readable

Implementing classes include: Reader

A source of characters that can be read.

Method summary
int read(CharBuffer cb)

Attempts to read characters from the buffer specified by the argument.

3.2 Files and pathnames


Class File
[Link]
[Link]

public class File extends Object


implements Comparable<File>

An abstract representation of file and directory (folder) pathnames.

Constructor summary
File(String pathname)

Constructs a File instance by converting the given pathname string into an abstract pathname.

Method summary
boolean canRead()

Returns true if the application can read the file denoted by this object, false otherwise.

boolean canWrite()

Returns true if the application can modify the file denoted by this object, false otherwise.

boolean exists()

[Link] 28/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Returns true if the file or directory denoted by this object exists, false otherwise.

boolean isDirectory()

Returns true if the file denoted by this object is a directory, false otherwise.

boolean isFile()

Returns true if the file denoted by this object is a normal file, false otherwise.

String toString()

Returns the pathname string of this object.

3.3 Reading from character streams


These classes are used for reading character data from input streams. They implement the Readable
interface, which means that they have a method to read from a character buffer, and they implement the
Closeable and AutoCloseable interfaces, which means that they have the close() method.

Class Reader
[Link]
[Link]

public abstract class Reader extends Object


implements Readable, Closeable, AutoCloseable

An abstract class for reading character streams. Instances of subclasses of the Reader class handle (16-
bit) character streams; this means that they correctly handle textual information based on characters and
strings.

Method summary
abstract void close() throws IOException

Closes the stream.

int read() throws IOException

Reads a single character. Returns the character read, as an integer in the range 0 to 65535, or -1 if
the end of the stream has been reached.

int read(char[] cbuf) throws IOException

[Link] 29/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Reads characters into an array specified by the argument cbuf. Returns the number of characters
read, or -1 if the end of the stream has been reached.

abstract int read(char[] cbuf, int offSet, int length) throws IOException

Reads characters into a portion of cbuf, storing the first character at offSet and reading length
characters. Returns the number of characters read, or -1 if the end of the stream has been reached.

int read(CharBuffer target) throws IOException

Attempts to read characters into the specified character buffer target. Returns the number of
characters added to the buffer, or -1 if this source of characters is at its end.

long skip(long n) throws IOException

Skips n characters.
Class FileReader
[Link]
[Link]
[Link]
[Link]

public class FileReader extends InputStreamReader


implements Readable, Closeable, AutoCloseable

The simplest Reader subclass to use to open an input stream to read characters from a text file.

Constructor summary
FileReader(File file) throws FileNotFoundException

Constructs a new FileReader, given the File to read from.

Method summary
See the Reader abstract class.

Class BufferedReader
[Link]
[Link]
[Link]

public class BufferedReader extends Reader


implements Readable, Closeable, AutoCloseable

[Link] 30/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Reads text from a character-input stream, buffering characters for efficiency. In general, each read request
made of a Reader causes a corresponding read request to be made of the underlying character or byte
stream. It is therefore advisable to use a BufferedReader to wrap any Reader whose read() operations
may be costly, such as instances of FileReader.

Constructor summary
BufferedReader(Reader in)

Constructs a buffered character-input stream.

Method summary
See also the Reader abstract class.

String readLine() throws IOException

Reads a line of text. A line is considered to be terminated by any one of a linefeed ('\n'), a carriage
return ('\r') or a carriage return followed immediately by a linefeed. Returns a String containing
the contents of the line, not including any line-termination characters, or null if the end of the stream
has been reached.
Class Scanner
[Link]
[Link]

public final class Scanner extends Object


implements Closeable, Iterator<String>, AutoCloseable

A simple text scanner that can parse primitive types and strings using regular expressions. A Scanner
breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting
tokens may then be converted into values of different types using the various next methods.

Constructor summary
Scanner(File source) throws FileNotFoundException

Constructs a Scanner that produces values scanned from the file specified by the argument.

Scanner(Readable source)

Constructs a Scanner that produces values scanned from the source specified by the argument.
Note that the Reader class implements the Readable interface so subclasses of Reader can be
wrapped by a scanner.

Scanner(String source)

[Link] 31/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Constructs a Scanner that produces values scanned from the string specified by the argument.

Method summary
void close()

Closes this scanner.

Pattern delimiter()

Returns the Pattern this scanner is currently using to match delimiters.

boolean hasNext()

Returns true if this scanner has another token in its input.

boolean hasNextInt()

Returns true if the next token in this scanner’s input can be interpreted as an int value, false
otherwise.

The following similar methods are also available: hasNextBigDecimal(), hasNextBigInteger(),


hasNextBoolean(), hasNextByte(), hasNextDouble(), hasNextFloat(), hasNextLong(),
hasNextShort().

boolean hasNextLine()

Returns true if there is another line in the input from this scanner.

String next()

Finds and returns the next complete token from this scanner.

int nextInt()

Scans and returns the next token of the input as an int.

The following similar methods are also available: nextBigDecimal(), nextBigInteger(),


nextBoolean(), nextByte(), nextDouble(), nextFloat(), nextLong(), nextShort().

String nextLine()

Advances this scanner past the current line and returns the input that was skipped as a string.

[Link] 32/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Scanner useDelimiter(String pattern)

Sets this scanner’s delimiting pattern to a pattern constructed from the String argument. Returns
this scanner.

3.4 Writing to character streams


These classes are used for writing character data to output streams. They implement the Appendable
interface, which means that they have the append(char) method, the Closeable interface (and thus the
AutoCloseable interface), which means that they have the close() method, and the Flushable
interface, which means that they have the flush() method.

Class Writer
[Link]
[Link]

public abstract class Writer extends Object


implements Appendable, Closeable, Flushable, AutoCloseable

An abstract class for writing to character streams. Instances of subclasses of the Writer class handle (16-
bit) character streams; this means that they correctly handle textual information based on characters and
strings.

Method summary
In the following methods, where the formal argument is of type CharSequence, you can assume for our
purposes that the actual argument is a String object or a StringBuilder object.

Writer append(char c) throws IOException

Appends the argument character c to this object. Returns this object.

Writer append(CharSequence csq) throws IOException

Appends the argument character sequence csq to this object. Returns this object.

Writer append(CharSequence csq, int start, int end) throws IOException

Appends a subsequence of the argument character sequence csq to this object, where start is the
index of the first character in the subsequence and end is the index of the character following the last
character. Returns this object.

abstract void close() throws IOException

[Link] 33/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Closes the stream, flushing it first.

abstract void flush() throws IOException

Flushes the stream. If the stream has saved any characters from the various write() methods in a
buffer, they are written immediately to their intended destination. Then, if that destination is another
character or byte stream, it is flushed. Thus one flush() invocation flushes all the buffers in a chain
of Writers and OutputStreams. If the intended destination of this stream is an abstraction provided
by the underlying operating system, for example a file, flushing the stream guarantees only that bytes
previously written to the stream are passed to the operating system for writing; it does not guarantee
that they are actually written to a physical device such as a disk drive.

void write(char[] cbuf) throws IOException

Writes the argument array of characters cbuf.

abstract void write(char[] cbuf, int offSet, int length) throws IOException

Writes length characters of the array cbuf starting with the character in index position offSet.

void write(int c) throws IOException

Writes a single character.

void write(String str) throws IOException

Writes a string.

void write(String str, int off, int len) throws IOException

Writes length characters of the string str starting with the character in index position offSet.
Class FileWriter
[Link]
[Link]
[Link]
[Link]

public class FileWriter extends OutputStreamWriter


implements Closeable, Flushable, Appendable, AutoCloseable

The simplest Writer subclass to use to open an output stream to write characters to a text file.

[Link] 34/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Constructor summary
FileWriter(File file) throws IOException

Constructs a FileWriter object given a File object. Anything written to file is added at the
beginning, therefore overwriting any existing contents.

FileWriter(File file, boolean append) throws IOException

Constructs a FileWriter object given a File object. If append is true, anything written to file is
added at the end. If append is false, anything written to file is added at the beginning, therefore
overwriting any existing contents.

Method summary
See the Writer abstract class.
Class BufferedWriter
[Link]
[Link]
[Link]

public class BufferedWriter extends Writer


implements Closeable, Flushable, Appendable, AutoCloseable

Writes text to a character-output stream, buffering characters to provide for the efficient writing of single
characters, arrays and strings. It is advisable to use a BufferedWriter to wrap any Writer whose
write() operations may be costly, such as instances of FileWriter.

Constructor summary
BufferedWriter(Writer out)

Constructs a buffered character-output stream that uses a default-sized output buffer.

Method summary
See also the Writer abstract class.

void newLine() throws IOException

Writes a line separator. This method uses the platform’s own notion of line separator as defined by
the system property [Link]. Using this method to terminate each output line is therefore
preferred to writing a newline character directly.

4 Exceptions

[Link] 35/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

The most important examples of error and exception classes are listed in this section. All of these classes
have a zero-argument constructor as well as a constructor taking a String as an argument (which is used
as the error or exception message).

Class Throwable
[Link]
[Link]

public class Throwable extends Object

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects
that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be
thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the
argument type in a catch clause.

Method summary
Throwable getCause()

Returns the cause of this throwable or null if the cause is non-existent or unknown.

String getMessage()

Returns the detailed message string of this throwable.

void printStackTrace()

Displays information about this throwable and its stack trace.

String toString()

Returns a short description of this throwable.

Class AssertionError
[Link]
[Link]
[Link]
[Link]

public class AssertionError extends Error

This class of error is signalled to indicate that an assertion has failed.

[Link] 36/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Method summary
See the Throwable class.
Class Exception
[Link]
[Link]
[Link]

public class Exception extends Throwable

The class Exception and its subclasses are a form of Throwable that indicates conditions that an
application might reasonably want to catch. All direct subclasses of Exception (except
RuntimeException and its subclasses) are checked exceptions.

Method summary
See the Throwable class.

4.1 Checked exceptions


Class FileNotFoundException
[Link]
[Link]
[Link]
[Link]
[Link]

public class FileNotFoundException extends IOException

Signals that an attempt to open the file denoted by a specified pathname has failed. This exception is
thrown by the FileInputStream and FileOutputStream constructors when a file with the specified
pathname does not exist. It is also thrown by these constructors if the file does exist but for some reason is
inaccessible, for example when an attempt is made to open a read-only file for writing.

Method summary
See the Throwable class.

Class IOException
[Link]
[Link]
[Link]
[Link]

public class IOException extends Exception

[Link] 37/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions
produced by failed or interrupted I/O operations.

Method summary
See the Throwable class.

4.2 Unchecked exceptions


Class RuntimeException
[Link]
[Link]
[Link]
[Link]

public class RuntimeException extends Exception

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation
of the Java Virtual Machine. A method is not required to catch any instances of subclasses of
RuntimeException that might be thrown during the execution of that method as these exceptions are
unchecked exceptions.

Method summary
See the Throwable class.

Class ArrayIndexOutOfBoundsException
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

public class ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or
greater than or equal to the size of the array.

Method summary
See the Throwable class.

Class ArithmeticException
[Link]
[Link]

[Link] 38/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

[Link]
[Link]
[Link]

public class ArithmeticException extends RuntimeException


Thrown when an exceptional arithmetic condition has occurred. For example, an integer ‘divide by zero’
throws an instance of this class.

Method summary
See the Throwable class.
Class IllegalArgumentException
[Link]
[Link]
[Link]
[Link]
[Link]

public class IllegalArgumentException extends RuntimeException

Thrown to indicate that a method has been passed an illegal or inappropriate argument.

Method summary
See the Throwable class.

Class NullPointerException
[Link]
[Link]
[Link]
[Link]
[Link]

public class NullPointerException extends RuntimeException

Thrown when an application attempts to use null in a case where an object is required.

Method summary
See the Throwable class.

Class NumberFormatException
[Link]
[Link]
[Link]
[Link]

[Link] 39/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

[Link]
[Link]

public class NumberFormatException extends IllegalArgumentException

Thrown to indicate that the executing method has attempted to convert a string to one of the numeric
types, but the string does not have the appropriate format.

Method summary
See the Throwable class.
Class StringIndexOutOfBoundsException
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException

Thrown to indicate that a string has been accessed with an illegal index. The index is either negative or
greater than or equal to the size of the string.

Method summary
See the Throwable class.

5 Java operators used in M250


The following table shows the Java operators used in M250, shown in order of precedence – from highest
to lowest.

Priority Operator Precedence

1 postfix ++ --

2 unary - !

3 multiplicative * / %

4 additive + -

5 relational < > <= >= instanceof

6 equality == !=

7 logical AND &&

[Link] 40/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

8 logical OR ||

9 assignment =

6 Java keywords
The following table includes all the keywords in the Java language, version 1.8.

abstract continue for new switch

assert default goto package synchronized

boolean do if private this

break double implements protected throw

byte else import public throws

case enum instanceof return transient

catch extends int short try

char final interface static void

class finally long strictfp volatile

const float native super while

[Link] 41/42
07/01/2026, 12:55 M250 Java Reference: View as single page | OU online

[Link] 42/42

You might also like