All classes that inherit from class Exception but not directly or indirectly from class RuntimeException
Unchecked exception
All classes that are subclasses of RuntimeException (typically caused by defects in your program's code) or Error (typically 'system' issues)
Unchecked exceptions are the subject of controversy
Exceptions are part of an API documentation and contract
User defined exceptions
Must be a child of Throwable
Checked exception needs to extend the Exception class, but not directly or indirectly from class RuntimeException
Unchecked exception (like a runtime exception) needs to extend the RuntimeException class
If a subclass method overrides a superclass method, a subclass's throws clause can contain a subset of a superclass's throws clause. It must not throw more exceptions!
Programmers should handle checked exceptions
If unchecked exceptions are expected, you must handle them gracefully
Only the first matching catch is executed, so select your catching class(es) carefully
Assertion
A statement in Java that enables you to test your assumptions about your program
Assertions should not be used for argument checking in public methods or to do any work that your application requires for correct operation
Evaluating assertions should not result in side effects
Java disables assertion validation feature by default, it needs to be explicitly enabled
Generics in Java
Enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods
Benefits of Generics
Removes casting and offers stronger type checks at compile time
Allows implementations of generic algorithms, that work on collections of different types, can be customized, and are type safe
Adds stability to your code by making more of your bugs detectable at compile time
Generic type
A generic class or interface that is parameterized over types
Defining a generic class
class name< T1, T2, ..., Tn > { /* ... */ }
Common type parameter names
E - Element
K - Key
N - Number
T - Type
V - Value
S,U,Vetc. - 2nd, 3rd, 4th types
Generic classes
Can have multiple type parameters
Generic methods
Methods that introduce their own type parameters
Collections framework
A unifiedarchitecture for representing and manipulating collections. A collection is simply an object that groups multiple elements into a single unit.
Collections framework contains
Interfaces - allows collections to be manipulated independently of the details of their representation
Implementations - concrete implementations of the collection interfaces
Algorithms - methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces
Core collection interfaces
Collection
Set
List
Deque
Map
Collection interface
Represents a group of objects known as its elements. Used to pass around collections of objects where maximum generality is desired.
Methods in the Collection interface
size()
isEmpty()
contains(Object element)
add(E element)
remove(Object element)
iterator()
General purpose collection implementations
HashSet
TreeSet
LinkedHashSet
ArrayList
LinkedList
ArrayDeque
HashMap
TreeMap
LinkedHashMap
Lambda expressions
Allow us to easily define anonymous methods, treat code as data and pass functionality as method argument
Anonymous inner class
Can be replaced by a lambda expression
Functional Interfaces
Interfaces with only one abstract method, that can be implemented using lambda expressions
Lambda expressions
Less verbose and offer more flexibility
Lambda expression syntax
Comma-separated list of formal parameters enclosed in parentheses, arrow token (->) and a body (single expression or statement block)
Method References
Treating an existing method as an instance of a Functional Interface, using :: operator
Method References
Static method (ClassName::methName), instance method of a particular object (instanceRef::methName or ClassName::methName), class constructor reference (ClassName::new)
Functional Interfaces
Provide predefined target types for lambda expressions and method references, each with a single abstract method
Functional Interfaces
Function (unary function from T to R), Consumer (unary function from T to void), Predicate (unary function from T to boolean), Supplier (nilary function to R)
Using Functional Interfaces
Comparator using lambda expression, Printing using lambda expression