Home > Back-end >  Java instanceof generics are involved when no inheritance compile error?
Java instanceof generics are involved when no inheritance compile error?

Time:12-03



Public class Test {
Public static void main (String [] args) {
List AList=new ArrayList<> (a);
A. A=new A ();
System. The out. Println (aList instanceof A);//compile can pass?
System. Out.println (aList instanceof ListSystem. Out.println (aList instanceof ListSystem. Out.println (a instanceof List<? & gt;);//compile can pass?
System. The out. Println (a instanceof List);//compile can pass?
//A and the List is not the same inheritance tree, why to compile?
}
}

Class A {
}

Class B {
}

1. A and the List without inheritance, why compilation can pass?
2. AList instanceof List
Why don't compiled by?

CodePudding user response:

https://www.cnblogs.com/lzq198754/p/5780426.html
Compile without reason, generic erasure mechanism, so the error
Compiled by the reason, why don't you through, it's not assert, won't produce an error, true or false, the only result instanceof judge is equivalent to only. The equals realized in the end

CodePudding user response:

List
AList=new ArrayList<> (a);
A. A=new A ();
B B=new B ();
System. The out. Println (instanceof b A);//A and B without inheritance, so compiled by not
System. The out. Println (aList instanceof A);//the List and A also does not have inheritance relationship, why can pass?

CodePudding user response:

reference 1st floor lkj2016 response:

https://www.cnblogs.com/lzq198754/p/5780426.htmlCompile without reason, generic erasure mechanism, so the error
Compiled by the reason, why don't you through, it's not assert, won't produce an error, true or false, the only result instanceof judge is equivalent to only. The equals in the low-end implements


Can you explain it again
List
AList=new ArrayList<> (a);
A. A=new A ();
B B=new B ();
System. The out. Println (instanceof b A);//A and B without inheritance, so compiled by not
System. The out. Println (aList instanceof A);//the List and A also does not have inheritance relationship, why can pass?

CodePudding user response:

Look at baidu instanceof himself, and the other mixed up, not compiled through, sign up for false, the running stage output without inheritance false, how could compile phase error,

CodePudding user response:

refer to the original poster DSQ2544404641 response:
1. A and the List without inheritance, why compilation can pass?
2. AList instanceof List
Why don't compiled by?


Upstairs said right

For example,

List
AList=new ArrayList<> (a);
List BList=new ArrayList<> (a);
System. The out. Println (aList. GetClass ());//output class Java. Util. ArrayList
System. The out. Println (aList. GetClass ()==bList. GetClass ());//output: true

So aList instanceof List
List< here; A> No sense, is considered to be grammatical errors,

CodePudding user response:

reference 4 floor lkj2016 response:
look at baidu instanceof himself, and the other mixed up, not compiled through, sign up for false, the running stage output without inheritance false, how could compile phase error,

You can copy the code you see, the content of the instanceof baidu, I find a lot of didn't find I said this problem

CodePudding user response:

The
reference 3 floor DSQ2544404641 response:
can you explain it again
List
AList=new ArrayList<> (a);
A. A=new A ();
B B=new B ();
System. The out. Println (instanceof b A);//A and B without inheritance, so compiled by not
System. The out. Println (aList instanceof A);//the List and A also does not have inheritance relationship, why can pass?


A Boolean result=obj instanceof Class

Note: the compiler will check whether obj can convert to the right of the class type, if you cannot convert an error directly, if you can't determine the type, compile, specific look at run time,

The List is a universal type by compiling so, no

CodePudding user response:

Instanceof is used to detect the instance belongs to the class, isn't it, and whether inheritance seems to be ok

CodePudding user response:

First suggest you look to the instanceof usage, A instanceof B, A is B type, pay attention to is the type, not the instance
  • Related