Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is null a class in java?

Tags:

java

null

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.

like image 734
Somerandomeguy Avatar asked Apr 07 '10 06:04

Somerandomeguy


People also ask

Can a class be null?

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...

Is null a type in Java?

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.

Is null an object in Java?

3. Type of null: Unlike the common misconception, null is not Object or neither a type.

What null means in Java?

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.


1 Answers

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.

like image 177
Dean Harding Avatar answered Oct 23 '22 22:10

Dean Harding