Search code examples
javainterfacetemporaryimplements

Find out implementation type of input interface


I have a function which has the interface Comparable as input parameter.

int sort(Comparable A[]){...}

Now I want to make a temporary variable of that type of which A is at run time (might be Integer or Float, but I don't know), to do comparisons for my sorting algorithm.
How is it done? I'm a freshman to Java.


Solution

  • First, please don't use Raw Types. Second, I don't think you can but you could make a temporary variable of Comparable. Something like,

    static <T> void sort(Comparable<T> A[]){
        if (A == null || A.length == 0) {
            return;
        }
        Comparable<T> temp = A[0];
        // ...
    }