What is the meaning of the rules for the Java build command Java?
When drilling a statement instanceof
at the Java bytecode build instruction level, it matches the Java build instruction instanceof
, But I read the rules used for
determine if an objectref that is not null is an instance of a permitted type:
Second rule:
If S is an interface type, then:
- If T is a class type, then T must be Object.
- If T is an interface type, then T must be the same interface as S, or the superinterface of S.
This confused me. Does "S - interface type" mean that the reference type of S is the interface type? if this is the first rule "If T is a class type, then T must be Object" cannot be true. For example,
CharSequence charSequence = new StringBuilder("test");
System.out.println(charSequence instanceof StringBuilder);
System.out.println(charSequence instanceof String);
the second line in the above code will print true, while the third line above will print false. So I doubt my understanding is probably wrong, anyone can help explain the meaning of the above rule?
source to share
You have the right to be confused as the way these rules were written is confusing.
It is not possible for objectref to have an interface type, since every instance-object is of an actual, not abstract type, possibly implementing the interface. This even applies to instances generated for lambda expressions that are of undefined (anonymous) type that implements a functional interface.
So, at first glance it seems that this part of these rules does not make sense. But consider the full text:
The following rules are used to determine whether an objectref that is not an
null
instance of a permitted type: ifS
is the class of the object referenced by objectref andT
is a permitted class, array, or interface type instanceof determines whether an objectref is an instanceT
as follows:
- If
S
is a regular (nonarray) class, then:
- If
T
is a class type, itS
must be the same class asT
, orS
must be a subclass of T;- If
T
is an interface type, thenS
must implement the interfaceT
.- If
S
is an interface type, then:
- If
T
is a class type, then itT
must be Object.- If
T
is an interface type, then itT
must be the same interface asS
, or a superinterfaceS
.- If
S
is a class representing an array typeSC[]
, that is, an array of type componentsSC
, then:
- If
T
is a class type, then itT
must be Object.- If
T
is an interface type, then itT
must be one of the interfaces implemented by arrays (JLS ยง4.10.3).- If
T
is an array typeTC[]
, that is, an array of type componentsTC
, then one of the following must be true:
TC
andSC
are the same primitive type.TC
andSC
are reference types, and the typeSC
can be translated inTC
these run-time rules.
Since it is not possible to have an interface type for the object referenced by objectref, only the other two bullets apply; its type is either "regular (non-Arrean) class" or an array type. In the latter case, the last sentence is interesting because it refers to the stated rules in general, which should apply to the types of components T
and S
if both are arrays of a reference type. And the component type can be interface type.
So, you can check these rules by using an actual instance of an interface type array, checking other array types:
Object o = new Collection[0]; // SC is Collection
System.out.println(o instanceof Object[]); // TC is class type Object -> true
System.out.println(o instanceof String[]); // TC is class type other than Object -> false
System.out.println(o instanceof Collection[]); // TC == SC -> true
System.out.println(o instanceof Iterable[]); // TC is super interface of SC -> true
System.out.println(o instanceof List[]); // TC is not super interface SC -> false
It seems that it would be less confusing if the interface was described in a special case of an array where it can be used. On the other hand, these three cases follow general formal designation rules, so they are easier to recognize in this form.
source to share