ç·ç§°åã®instanceof
instanceofã§ã¯ã
- åãã©ã¡ã¼ã¿ãã¤ãã¦ããªãå(ä¾:List)
- ä¸éorä¸éã®æå®ã®ãªãã¯ã¤ã«ãã«ã¼ããæå®ãããå(ä¾:List<?>)
ã®ãããããæå®ã§ãã¾ãã
- å
·ä½çãªåããã©ã¡ã¼ã¿ã¨ãã¦æã¤ãã®(extendsãsuperãå«ã)ã¯æå®ã§ãã¾ããã
- âããã¯ãå®è¡æã«åãã©ã¡ã¼ã¿ã®æ£ç¢ºãªå¤ãåå¾ã§ããªããããã¨ã®ãã¨ã
- 許å¯ããã¦ãã2ã¤(ãreifiable typesãã¨å¼ã°ããã)ã¯ãã³ã³ãã¤ã«æã«åæ å ±ã失ãããªãã®ã§æå®å¯è½ãããã(Java Generics FAQs : Cast and instanceofãã)
Object obj = new ArrayList<Exception>(); System.out.println( obj instanceof List ); System.out.println( obj instanceof List<?> ); // ããã¯OK // System.out.println( obj instanceof List<Exception> ); // ã³ã³ãã¤ã«ã¨ã©ã¼ // System.out.println( obj instanceof List<? extends Exception> ); // ã³ã³ãã¤ã«ã¨ã©ã¼ // System.out.println( obj instanceof List<? super Exception> ); // ã³ã³ãã¤ã«ã¨ã©ã¼
å®è¡çµæã§ãã
true true
ã¡ãªã¿ã«ããããªã®ããã¡ã§ãã
static <X> void test( Object obj ) { // 以ä¸ã¯ã³ã³ãã¤ã«ã¨ã©ã¼ //System.out.println( obj instanceof List<? extends X> ); //System.out.println( obj instanceof List<X> ); }