According to, http://www.freshvanilla.org:8080/display/www/Java+Interview+Questions
Under
Which class is the superclass of every class?
null
seems to be the answer.
I found that
new Object().getClass().getSuperClass()
verifies the answer as correct.
But can null
be considered a class?
I see all primitive data types are represented as Class
objects from
java.lang.Class's documentation.
A class is never null - a reference to an instance may be null. The design question is: Who creates the instance? You could make it return an Option<'T> instead of a 'T. Then users cannot ignore the fact that sometimes there is None...
null is not an Object or neither a type. It's just a special value, which can be assigned to any reference type. Typecasting null to any reference type is fine at both compile-time and runtime and it will not throw any error or exception.
3. Type of null: Unlike the common misconception, null is not Object or neither a type.
In Java programming, null can be assigned to any variable of a reference type (that is, a non-primitive type) to indicate that the variable does not refer to any object or array.
Remember that the Class
class is itself a class. So when you call c.getClass()
you're getting back an instance of the Class
class. So because there is no super class of Object
, the getSuperClass()
method cannot return anything so it returns null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With