Udun's Labs

Java Memorabilia

Remember erasure ?

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
        ((List) strings).add(42); // compiler warning - ok at runtime
        // String s = strings.get(0); //  CCE - Compiler-generated cast
        System.out.println("strings: " + strings); // strings: [42]
        System.out.println(strings.contains(42)); // true
    }
}

In other words:

    final HashSet<Integer> hashSet = new HashSet<>();
    // Set<String> cast = (Set<String>) hashSet; // error
    Set<String> cast = (Set) hashSet; // warning

From: here and here

Remember printing (and varargs) ?

Arrays.toString(map.entrySet().toArray());

as opposed to:

int[] a = { 1, 2, 3 }; System.out.println(Arrays.asList(a)); // \[\[I@70cdd2\] // List<int[]>

Remember generics ?

Trivia

You might want to visit my sponsors:

Comments