Java generics problem

this problem requires a deep understanding of generics. The principle is clear, but I just don"t understand the problem I"ve explained below

the code is as follows

public class Shape implements Comparable<Shape> {
    @Override
    public int compareTo(Shape o) {
        return 0;
    }
}

public class Square extends Shape {
}

public class TestGenetic {
    public static <AnyType extends Comparable<? super AnyType>> AnyType findMax(AnyType[] arr){
        int maxIndex=0;
        for (int i = 0; i <arr.length ; iPP) {
            if (arr[i].compareTo(arr[maxIndex+1])>0)
                maxIndex=i;
        }
        return arr[maxIndex];
    }
    public static void main(String[] args) {
        Shape[] shapes={new Shape(),new Shape()};
        Square[] squares={new Square(),new Square()};
        findMax(shapes);
        findMax(squares);
    }
}

if you modify generics in generic methods:

public class TestGenetic {
    public static <AnyType extends Comparable<AnyType>> AnyType findMax(AnyType[] arr){
        int maxIndex=0;
        for (int i = 0; i <arr.length ; iPP) {
            if (arr[i].compareTo(arr[maxIndex+1])>0)
                maxIndex=i;
        }
        return arr[maxIndex];
    }
    public static void main(String[] args) {
        Shape[] shapes={new Shape(),new Shape()};
        Square[] squares={new Square(),new Square()};
        findMax(shapes);
        findMax(squares);
    }
}

will not report an error either

however, this is not the case in generic classes

public class TestGeneric1<AnyType extends Comparable<? super AnyType>> {

    public static void main(String[] args) {
        TestGeneric1<Square> p=null;
    }
}

this won"t report an error, but if I modify the generics, it looks like this:

public class TestGeneric1<AnyType extends Comparable<AnyType>> {

    public static void main(String[] args) {
        TestGeneric1<Square> p=null;//
    }
}

then the compilation error is reported. Why is there a generic check in the generic class, but not in the generic method? in the generic method, Square obviously does not meet the condition of AnyType extends Comparable < AnyType >

.

Please give me your advice, thank you

Mar.01,2021

will be checked, but the requirements are different.

The generic parameters of the

method are actually checked in two places: when called and when the return value is obtained.

your example only calls the method and does not get its return value, so it is only checked once.

the check for calling parameters is relatively loose, and it only checks whether the passed parameters conform to the lower bound of the method generics .

is more stringent in checking the return value, which checks whether the receiving type of the return value conforms to the upper bound of the method generics . This is the same as when declaring class objects.

specifically, when calling a method, if I pass in a Square , but the method generic requires a Shape (via < AnyType extends Comparable < AnyType > > ). In this case, you can compile, because the compiler treats this Square parameter as Shape , that is, the upward transformation. This is not a problem at all, because any subclass of Shape is Shape .

however, it's different when you get the return value, and if you get it with a Shape variable, it's fine, because it meets the requirements of generics. But not if you use a Square variable to get it, because the method defined in this way can only return Shape , so it cannot automatically transition down to Square .

declares a class object in the same way as getting the return value of a method.

so in your example, after modification, declare that the class object cannot be compiled, but the method call can. If you test the return value, you will find that the situation is the same as the declaration of the class object.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea1791-47922.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea1791-47922.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?