Operator Precedence

The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence.

When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

Unary operators act upon operand, binary operators act upon two operands, and the ternary operator acts upon three operands.

[Resource] [Resource]

Operators Precedence
postfix expr++ expr–
unary ++expr –expr +expr -expr ~ !
multiplicative * / %
additive + –
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

Collection (Interface)

The Collection root interface in the collection hierarchy, and is used to represent a group of elements. All classes that implement Collection have two constructors: a void constructor, which creates an empty collection, and a constructor with a single argument of type Collection, which creates a new collection with the same elements as its argument (making a copy).

[Resource]

Return Method Description
boolean add(E e) Ensures that element is in collection.
boolean addAll(Collection c) Adds all of the elements in the specified collection to this collection.
void clear() Removes all of the elements from this collection.
boolean contains(Object o) Returns true if this collection contains the specified element.
boolean containsAll(Collection c) Returns true if this collection contains all of the elements in the specified collection.
boolean equals(Object o) Compares the specified object with this collection for equality.
int hashCode() Returns the hash code value for this collection.
boolean isEmpty() Returns true if this collection contains no elements.
Iterator iterator() Returns an iterator over the elements in this collection.
boolean remove(Object o) Removes a single instance of the specified element from this collection, if it is present.
boolean removeAll(Collection c) Removes all of this collection’s elements that are also contained in the specified collection
boolean retainAll(Collection c) Retains only the elements in this collection that are contained in the specified collection
int size() Returns the number of elements in this collection.
Object[] toArray() Returns an array containing all of the elements in this collection.
T[] toArray(T[] a) Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

Optional implementation: add(), addAll(), clear(), remove(), removeAll(), and retainAll().
Required implementation: contains(), containsAll(), equals(), hashCode(), isEmpty(), iterator(), size(), toArray(), and toArray(T[] a).

Polymorphism

Polymorphism is an object oriented term that is used to describe the implementation of a single interface to multiple objects. The three different types of polymorphism are: ad hoc polymorphism, parametric polymorphism, and subtype polymorphism(AKA inclusion polymorphism).

Ad Hoc Polymorphism

Ad Hoc Polmorphism occurs when the same operation or method behave differently depending on the data types that are passed as arguments.

Example:

5 + 5 = 10;
"Hello" + "World" = "HelloWorld";

This demonstrates how the addition operator is polymorphic because when using integers, it performs an addition, and when using Strings, it performs a concatenation–two different operations for two different data types using the same operator.

Parametric Polymorphism

Parametric polymorphism occurs when an interface can be adapted to different data types or methods through the use of Generics.

Example:

ArrayList list = new ArrayList;
Typically, one would use “” to specific the collection’s datatype, yet in this polymorphic case, it is unnecessary. ArrayList can be used for any data type given the Generic declaration is appropriate; hence, only one interface is being used for many different types of objects because of this generic implementation.

Subtyping

Subtype Polymorphism occurs when an interface limits the amount of polymorphic types that can be used, and is typically implemented via abstract object methods.

Example:

abstract class Animal {
 abstract String noise();
}
class Dog extends Animal {
 public String noise() {
  return "Bark";
 }
}
class Cat extends extends {
 public String noise() {
  return "Meow";
 }
}
public class Runner {
 public void makeNoise(Animal a) {
  System.out.print(a.noise());
 }
 public static void main(String[] args) {
  makeNoise(new Cat());
  makeNoise(new Dog());
 }
}

In this example, the Dog will output Bark while the Cat will output Meow. This demonstrates subtype polymorphism because the parent Animal type will be differentiated based on subtype implementations.

Binary Search

Binary Search is a searching algorithm for sorted lists, and it works by comparing the search key value with the key value of the middle element of the list. If the keys match, then a matching element has been found and its index, or position, is returned. Otherwise, if the search key is less than the middle element’s key, then the algorithm repeats its action on the sublist to the left of the middle element or, if the search key is greater, on the sublist to the right. If the remaining list to be searched is empty, then the key value is not present in the list.

[Example] [Example]
[Resource] [Resource]

Performance
Worst case performance O(log n)
Best case performance Ω(1)
Average case performance O(log n)

Step by Step

Quick Sort

Quick Sort is a sorting algorithm that works by selecting a pivot from a list, and then reordering the list so that all of the elements that are smaller than the pivot go to the left, and all of the elements that are larger than the elements go to the right. Quick sort then recursively applies this algorithm to each of the smaller/larger sublists until the pivots have reached their correct sorted position in the list. The point where a recursize operation terminates in a quick sort is called the partition operation.

[Example] [Example]
[Resource] [Resource]

Performance
Worst case performance O(n2)
Best case performance Ω(n log n)
Average case performance O(n log n)

Step by Step

Merge Sort

A Merge Sort is a sorting algorithm that works by dividing an unsorted list into sublists that all contain 1 element, and then by repeatedly merging these sublists until there is only 1 sorted list remaining. Merging sorted sublists(single element sublists are always sorted) is very efficient because it only requires one comparison between the current elements of either sublist to add an element to the merged list.

[Example] [Example]
[Resource] [Resource]

Performance
Worst case performance O(n log n)
Best case performance Ω(n log n)
Average case performance Θ(n log n)

Step by Step

Insertion Sort

Insertion sort is a sorting algorithm runs through each element of a list, consuming one element upon each repetition, and growing a sorted sublist. Upon each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

[Example] [Example]
[Resource] [Resource]

Performance
Worst case performance О(n2)
Best case performance Ω(n)
Average case performance О(n2)

Step by Step

Selection Sort

Selection Sort is a sorting algorithm that sorts a list from left to right by taking the smallest(or largest, depending on sorting order) unsorted value and swapping it with the leftmost unsorted value upon each iteration. Once the value is swapped with the leftmost unsorted values, it is then considered a sorted value, and after the algorithm has done this for every value in the list, then the entire list will have been sorted.

[Example] [Example]
[Resource] [Resource]

Performance
Worst case performance О(n2)
Best case performance Ω(n2)
Average case performance Θ(n2)

Step by Step

selection sort step by step

Bubble Sort (Sinking Sort)

A Bubble Sort is a sorting algorithm that compares two adjacent data at a time and swaps them if they are in the incorrect order. It runs through the list of data to be sorted until there are no more swaps that need to be done. It is a very slow yet simple algorithm.

[Example] [Example]
[Resource] [Resource]

Performance
Worst case performance O(n2)
Best case performance Ω(n)
Average case performance O(n2)

Step by Step

bubble sort step by step