简体   繁体   中英

Difference between Object[] and Array of specific Objects in java

I know this may be trivial but I could not find an answer yet.

What are the differences between and Object[] and an array of specific Objects, for example

class car {
    public Object[] someCars;
    public car[] someOthercars;
}

I know that car[] can store car Objects and Object[] can store any Objects. But APART from this are there any not so obvious differences?

Car[] can contain only Car instances, or sub-classes of Car instances. If you were to have a class like this: class Honda extends Car , Honda instances can be populated in the Car array as well, because Honda extends from Car .

Object[] can contain any Object instances, or any subclass of Object (every class in Java is a subclass Object , even if it is not explicitly stated with an extends keyword in a custom class), so any instance in java can be populated in the Object array.

Answers to your questions are given here:

  1. The Java™ Tutorials. Classes
  2. The Java™ Tutorials. Object-Oriented Programming Concepts

UPD

Car class inherits from Object . An array of Car s is narrower than an array of Object s. So roughly speaking if you store your cars in an array of objects - you will need to cast them back to cars each time you want to use an instance. I doubt that you really need that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM