Why does every object in Java implicitly extend java.lang.Object class?

前端 未结 10 2599
青春惊慌失措
青春惊慌失措 2020-11-29 04:21

I have been programming in Java for quite some time, but when I tried to explain what an java.lang.Object class is to a friend, I could not come up with more th

10条回答
  •  被撕碎了的回忆
    2020-11-29 04:51

    I think the most important use of Object is not to provide common methods like toString() but to provide a common type that would hold all reference types.

    C++ don't have an Object equivalent and people are still happy. But since Java don't have pointers and C++-like templates, Object is required to make implementations of Collections, etc. possible.

    See also on discussions on reference and primitive types.

提交回复
热议问题