-3

The Java Language Specification in Section 4.3.1 states

An object is a class instance or an array.

Why is it not

An object is a class instance and an array is an class instance.

Why is this? I am quite sure the designers of Java did not wake up in the mood for this differentiation, but they had some reasons for doing so. What are these reasons?

14
  • Only means that arrays are objects. Even arrays of primitives. Commented Nov 3, 2017 at 13:37
  • 4
    What do you mean why? That is the definition. It could have been something else, but it isn't. I don't understand the question. Commented Nov 3, 2017 at 13:38
  • That's the first thing you have to know about OOP. Commented Nov 3, 2017 at 13:38
  • 2
    @JuanCarlosMendoza I do not think it is a duplicate. I do not question that it is this way, but I wonder why. Commented Nov 3, 2017 at 13:41
  • 1
    @ErwinBolwidt I am quite sure that the designers of Java did not just say: Because I like it this way. They had a reason why they decided to do so. This is my question. Commented Nov 3, 2017 at 13:47

1 Answer 1

1

It becomes obvious if you read it in context

An object is a class instance or an array.

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

A class instance is explicitly created by a class instance creation expression (§15.9).

An array is explicitly created by an array creation expression (§15.10.1).

So this is why it's defined this way. The mode of their creation is different, and this is how you can define it in a sensible way.

When reading the JLS, it's always a good idea to read sections from start to finish as concepts are often introduced top-down: they're named first, then explained in the paragraphs that follow.

Of course you could do it the way you've suggested:

An object is a class instance and an array is a class instance too.

Class instances are explicitly created by a not-an-array class instance creation expression (§15.9) if they're not arrays or an array creation expression (§15.10.1) if they're arrays

Or you could find a name for these not-array-but-objects, let's call them class schminstance and we're back to square one.


If the question is about why arrays had to be created different in the first place (and not all be instances of an Array class for example), it's because Java originally didn't have generics/type parameters. But they needed something like it for arrays (otherwise you couldn't have methods that would sort any array for example), so this is what they've come up with. They also made arrays covariant (an object of type String[] is also an Object[], but an Object[] containing only strings is not a String[]), which makes them differ from generics.

Sign up to request clarification or add additional context in comments.

7 Comments

I dont think copy paste the same 4 lines from the JLS is really an answer... can you improve ?
@vikingsteve In this case it really is that simple. The JLS is very clear about why it talks about these two categories and there's not much to add to it.
@MichaelDorner Because they're created different. How else would you define them and their different modes of creation?
Just to show that there is an alternative, C# arrays are actually instances of class Array.
@biziclop In the JLS, as class instance is defined as an instance of a class. Classes are defined in Section 8 of the JLS. Interfaces are in Section 9, and Arrays in Section 10. Neither interfaces nor arrays are classes.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.