Definitions

Functions

  • Hides implementation details so they can be changed later without affecting other parts of the code
  • Allows for reuse of computations, makes code more succinct and easier to change

Statically typed language

  • Declare every variable used and its type
  • Type of variable cannot be changed after declaration

Strongly typed

  • Enforces strict rules in type system to ensure type safety
  • Only allow type casting if it makes sense

Method signature

  • method name, number of parameters, type of each parameter and its order
  • C::foo(B1, B2)

Method descriptor

  • Method signature + return type
  • A C::foo(B1, B2)

Overriding

  • Uses @Override annotation
  • Same method descriptor as parent class
  • Return type must be a subtype of the overridden method’s return type

Overloading

  • Same method name in the same class
  • Different method signature
    • Same method signature with different return type is not allowed
  • By either changing type, order or number of parameters but keeping the method name identical
  • Can overload constructors as well

Interface

  • Specifies a set of methods a class must implement
  • Methods are abstract by default

Abstract classes

  • Cannot be instantiated
  • Contains both abstract and concrete methods

Checked exceptions

  • Programmer has no control over exception
  • Handled with try, catch and finally blocks
  • Checked in order of catch blocks
  • Overriding a method that throws a checked exception must throw the same or more specific checked exception

Unchecked exceptions

  • Programmer has control over
  • Runtime Exceptions

Producer Extends, Consumer Super (PECS)

Parallel and Concurrent programming

  • Sequential: 1 instruction of program running at a time
  • Concurrency: divide computation into subtasks and switch between concurrent processes to give the illusion of running at the same tinme
    • Subtasks known as threads
  • Parallelism: subtasks running at the same time
    • With multiple cores/processors

All parallel programs are concurrent, but not all concurrent are parallel

Java concurrent programming

  • Threads
  • CompletableFuture
  • Fork and Join Pools
    • Collection of threads, each waiting for a task to execute, and a collection of tasks to be executed

OOP Principles

Encapsulation

  • Bundling of fields and methods into a class
  • Maintains abstraction barrier
  • Reference type stores the reference to the value, null if not initialised

Information Hiding

  • Private instance fields, public methods

Composition

  • has-a relationship between 2 classes
  • Aliasing: sharing same reference values

Tell don’t ask

  • Tell the object what to do instead of asking for its state and performing the task on its behalf

Inheritance

  • Ability to reuse code of existing super classes
  • is-a relationship

Polymorphism

  • Using same method signature in subclasses to determine behaviour for specific subclasses
  • Works due to dynamic binding, method invoked is decided during run-time, based on its run-time type

Liskov Substitution Principle (LSP)

  • Let (x) be a property provable about objects x of type T. Then (y) should be true for objects y of type S where S <: T
  • A subclass should not break the expectations set by the superclass
  • If a class B is substitutable for a parent class A, then it should be able to pass all test cases of the parent class A

Java syntax and the way it works

Class fields

  • Static fields: public static final double PI
  • Accessed with class name

Class methods

  • Cannot access instance fields or call other instance methods

Main method

  • public static final void main(String[] args)

Stack and Heap

Stack

  • Variables are allocated and stored here
  • Instance and class fields are not in the stack
  • Call frames are created when we invoke a method and removed when it completes (variables are contained within this)

Heap

  • Region in memory to allocate and store objects
  • New objects created in heap when the new keyword is used
  • Objects can persist across multiple methods and can be shared

Dynamic binding

  1. Determine compile-time type C of target
  2. Check all accessible methods in C (including inherited methods)
  3. Choose the most specific method (method M is more specific than method N if arguments to M can be passed to N w/o compilation error)
  4. Store the method descriptor in the bytecode
  5. Determine run-time type of target
  6. Look for an accessible method with matching method descriptor (first matching method descriptor found is chosen, find upwards)

Wrapper classes

  • Autoboxing: convert primitive to wrapper
  • Unboxing: convert wrapper to primitive
  • All primitive wrapper class objects are immutable
  • Comes at a performance issue, need to allocate memory for the object and clean it up

Generics

  • Parameterised types
  • Allows us to create classes, interfaces and methods where the type of the data is specified as a parameter
  • Prevent us from needing to write multiple versions of the same code for different data types

Bounded type parameters

  • class Pair<S extends Comparable<S>, T>

Type erasure

  • Generic types are erased after type checking
    • Generics and Arrays cannot mix
    • However, generic array declaration is still allowed, just not generic array instantiation

Unchecked warnings

  • @SuppressWarnings(“unchecked”)
  • Used only when we can guarantee that our code is type safe
  • Must be applied to the most limited scope and add comments

Raw types

  • Raw types throw unchecked warnings
  • Try not to use raw types

Type inferencing

  • Constraints
    • Target type
    • Argument type
    • Type parameter bound

Immutability

  • Advantages
    • Ease of understanding
    • Enabling safe sharing of objects
    • Enabling safe sharing of internals
    • Enable safe concurrent execution

Nested classes

  1. Inner class: access to outer class with all fields and methods
  2. Static nested class: inner class with only access to static parts
  3. Local class: within a function
  4. Anonymous class: local class without a name

Pure Functions

• Deterministic • Referential Transparency - replace f(x) with a everywhere, given a = f(x), and the converse holds too • No side effects - don’t print to the screen, write files, throw exceptions, change other variables and modify the values of the argument

@FunctionalInterface

• 1 abstract method to override • Method reference: origin::distanceTo, can refer to static method, instance method or constructor (Box::new)

Monads

  • of and flatMap methods

Functors

  • of and map methods