'Java document says cannot use instanceof in generic but it is working

Cannot Use Casts or instanceof with Parameterized Types

I am trying to understand the generics in java. I saw the restrictions of Generics in the oracle document. I wanted to try this rule which is above. When I use instanceof I didn't get any error but oracle doc says it will give the compile-time error. Can you explain, please?

doc: https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#cannotCast

My code:

public class Main {
    public static void main(String[] args) {
        Test.print("hello");
        Test.print(15);
    }
}

class Test {
    public static <E> void print(E obj1) {
        if (obj1 instanceof String) {
            System.out.println("This is String, " + obj1);
        } else if (obj1 instanceof Integer) {
            System.out.println("This is Integer, " + obj1);
        } else {
            System.out.println(obj1);
        }
    }
}

Outputs:

This is String, hello
This is Integer, 15


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source