/java-11-in-action

Upgade to Java 11

Primary LanguageJava

Java Core

Description

The project contains some notes and code examples to upgrade to Java 11 and to pass exam 1Z0-817.

Contents

Generics

  • Ambiguity errors that involve overloading methods:
class MyGenClass<T, V> {
    T ob1;
    V ob2;

    void set(T o) {
        ob1 = o;
    }

    void set(V o) {
        ob2 = o;
    }   
}
// these two overloaded methods are ambiguous and won't compile
// because type erasure results in both to 
// void set(Object o) {} 
  • Type parameter cannot be instantiated:
class Gen<T> {
    T ob;
    
    Gen() {
        ob = new T(); // Illegal!
    }   
}   
  • A generic class cannot extend Throwable. It is not possible to create generic exception classes.
  • Generic types can't be used by static members of classes:
class Wrong<T> {
    // no static variables of type T
    static T ob; 
    
    // no static methods that use T
    static T getOb() {
        return ob;
    }   
}
  • You cannot create array of type T:
...
T[] vals = new T[10]; // incorrect
  • You cannot create array of type-specific generic references:
MyClass<Integer> arr[] = new MyClass<Integer>[5]; // incorrect

but this OK:

MyClass<?> gens[] = new MyClass<?>[10];

Lambda Expressions

  • Lambda expression has access to instance and static members defined by enclosing class
  • Lambda expression has access to local variable and can use it, as for as this variable is effectively final. So neither inside lambda expression or outside this variable must not be modified, otherwise code won't compile

Collections

Collection Hierarchy The capacity is the number of buckets(used to store key and value) in the Hash table , and the initial capacity is simply the capacity at the time Hash table is created.

The load factor is a measure of how full the Hash table is allowed to get before its capacity is automatically increased.