The WeakReference class, monitoring memory leak and garbage collection in a Java application

Image
 Below is a Stack implementation that uses an internal resizeable array structure.  public class MyStack< T > implements Stack< T > { private static final int CAPACITY = 100 ; private Object[] array ; private int pos = 0 ; public MyStack () { this . array = new Object[ CAPACITY ] ; } @Override public void push ( T item) { if ( pos >= array . length / 2 ) { Object[] newArray = new Object[ pos * 2 ] ; System. arraycopy ( array , 0 , newArray , 0 , array . length ) ; array = newArray ; } array [ pos ++] = item ; } @Override public T pop () { if (isEmpty()) { throw new RuntimeException( "empty stack" ) ; } @SuppressWarnings ( "unchecked" ) T item = ( T ) array [ pos - 1 ] ; pos -= 1 ; return item ; } @Override @SuppressWarnings ( "unchecked" ) public T peek...

Revisiting Java Generics and Wildcard Types

Generics were added with Java 5. Before that you had to cast every object you read from a collection. With generics you tell the compiler what types of objects are permitted in collections. Then the compiler tells you when there is a problem.

If a class or interface has type parameters, then it is called a generic class/interface. Generic classes or interfaces can be used as "raw types". This is primarily for backward compatibility. We get a warning message from compiler when we use raw types.

List<Object> can be used but you cannot assign List<String> to this type. They are both subtypes of raw List type.
Set<Integer> set1 = new HashSet<>();
Set<Object> set3 = new HashSet<>();
// compile error.
set1 = set3;
But what if you don't care about the actual type of the collection elements? Lets say you want to have a method with 2 sets as parameter.

We can use Set<?> which means "set of any type".
public static void main(String[] args) {
        Set<Integer> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();

        calculate(set1, set2);

        Set<?> mySet = new HashSet<>();

        // compile error. You cannot do that when the type is uncertain.
        // you can do that by using raw types but you get a warning and it is not reccomended!
        // this means, wildcard type is useful to assign a reference to another
        mySet.add(new Integer(1));

        Set myRawSet = new HashSet();

        myRawSet.add(new Integer(2));

        // legal
        mySet = set1;
        mySet = set2;
    }

    private static void calculate(Set<?> set1, Set<?> set2) {
    }

Regarding arrays, it is illegal to create an array of a generic type. In Java, arrays and generics don't mix very well. 

Below is an example that illustrates the syntax of generics:
public class Test {

    // generic method
    public <T> T doSomething(T a, T b) {
        return null;
    }

    // generic method with bounded type
    public <T extends Comparable> T doSomething2(T a, T b) {
        return null;
    }

    class MyGenericClass<K> {

        public K operation() {
            return null;
        }
    }

    private void wildCardUnbounded() {
        MyGenericClass<?> variable = new MyGenericClass<>();

        MyGenericClass<Integer> variable2 = new MyGenericClass<>();

        variable = variable2;
    }

    private void wildCardBounded() {
        MyGenericClass<? extends Comparable> variable = new MyGenericClass<>();

        MyGenericClass<Object> variable2 = new MyGenericClass<>();

        // compile error: not comparable
        variable = variable2;

    }

    private void wildCardBounded2() {
        MyGenericClass<? super Comparable> variable = new MyGenericClass<>();

        MyGenericClass<Object> variable2 = new MyGenericClass<>();

        // legal because Object is super class of Comparable
        variable = variable2;
    }
}


Below is an example that uses bounded wildcard types:

public class UnionExample {

    public static <E> Set<E> union(Set<? extends E> s1,
                                   Set<? extends E> s2) {
        //..
        return null;
    }

    public static void main(String[] args) {
        Set<Integer> integers = Set.of(1, 2, 3);

        Set<Double> doubles = Set.of(1.2, 2.3, 4.5);

        Set<UnionExample> examples = new HashSet<>();

        // valid
        Set<Number> result = union(integers, doubles);

        // compile error: UnionExample is not a subclass of Number
        result = union(integers, examples);
    }
}

Differences between Java Generics and C# Generics

The most important difference seems to be the "type erasure" concept. In Java, generics are a compile time structure, in runtime the type of the object is erased (erasure) so you cannot have the type information at runtime. In C#, generics are also present at the CLR level, and you can get the type information at runtime.

It is an error in C# to create a Generic Type without providing any Type Arguments. Java permits creating Generic Types without providing any Type Arguments; these are called raw types. This is because of backward compatibility. Compiler gives a warning and using raw types is a bad practice for modern java code.

In Java you cannot use your type variable of a class inside a static method:

public class GenericWithStaticMethod<Tgt;{

    // compile error!
    public static void doSomething(T a) {

    }
}

You see this famous error message:

error: non-static class T cannot be referenced from a static context







Comments

Popular posts from this blog

Trie Data Structure and Finding Patterns in a Collection of Words

My Crappy Looking Solution to "Binary Tree Common Ancestor" Problem

A Graph Application in Java: Using WordNet to Find Outcast Words