isAbstract() Modifier returning Incorrect result - Why?
To my understanding the following code should print False
as output
However, when I ran this code it is printing True
as output.
From Java docs:
Return true if the integer argument includes the abstract modifier, false otherwise.
public class Test{
public static void main(String[] args) {
System.out.println(Modifier.isAbstract(byte[].class.getModifiers()));
}
}
Can some one help me understand this behavior ?
The Javadoc of int java.lang.Class.getModifiers() specifies what should be returned for some of the modifiers for array types (for example, the final
modifier is required to be true
and the interface
modifier is required to be false
). On the other hand, it doesn't specify what the abstract
or static
modifiers should be for array types, which means the decision to return true
or false
is not documented in the JDK. Therefore any implementation can choose to return either true
or false
.
int java.lang.Class.getModifiers()
Returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface; they should be decoded using the methods of class Modifier.
If the underlying class is an array class, then its public, private and protected modifiers are the same as those of its component type. If this Class represents a primitive type or void, its public modifier is always true, and its protected and private modifiers are always false. If this object represents an array class, a primitive type or void, then its final modifier is always true and its interface modifier is always false. The values of its other modifiers are not determined by this specification.
The modifier encodings are defined in The Java Virtual Machine Specification, table 4.1.
A hint to this behavior may be found in the JLS, 10.8. Class Objects for Arrays:
Every array has an associated Class object, shared with all other arrays with the same component type.
Although an array type is not a class, the Class object of every array acts as if: [snipped]
Under this reasoning, an array isn't a "real" class, so it definitely isn't a concrete class. The same logic would apply to int.class
being considered abstract.