Pythoner
Pythoner

Reputation: 5595

Are primitive types only types not inherited from java.lang.Object?

I'm reviewing the primitive types interview questions for a java role. I get asked such a statement during an test.

primitive type is any type that does not inherite from java.lang.Object.

Upvotes: 3

Views: 3346

Answers (2)

T. Claverie
T. Claverie

Reputation: 12256

There are other types which do not extend java.lang.Object. For example, null is a value, so it does not extend it. Interfaces do not extend it either, though abstract class do.

About primitive types, the answer is yes and no. When you write them, primitive type do not inherit from java.lang.Object. However, when compiling your java code to java bytecode, the compiler does something called autoboxing.

In short, at compile time the compiler transforms all the primitive values into their wrapper class (Integer for int, Boolean for boolean, etc). However, he does that efficiently, because it transforms int i = 0; into Integer i = Integer.valueOf(0);. Behind the scenes, the valueOf function of wrapper types is a Flyweight Factory, that is there is at most one instance of the Integer 0, one of the Integer 1, etc...

So, when coding you work with primitive types, but the JVM works with the wrapper of the primitive types, which implement java.lang.Object.

Upvotes: 10

foivaras
foivaras

Reputation: 248

No they are not. Interfaces also do not inherit from java.lang.Object

Upvotes: 2

Related Questions