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

Michael Dorner
  • 17,587
  • 13
  • 87
  • 117
  • Only means that arrays are objects. Even arrays of primitives. – Mena Nov 03 '17 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. – Sotirios Delimanolis Nov 03 '17 at 13:38
  • That's the first thing you have to know about OOP. – diiN__________ Nov 03 '17 at 13:38
  • @SotiriosDelimanolis I wonder why array is not a class instance; why this special role for arrays? – Michael Dorner Nov 03 '17 at 13:40
  • Why is it not "An object is a class instance and an array is an class instance:" -- because it isn't. An array is a `java.lang.Object`, but it's not an instance of a real class (although you can get a java.lang.Class instance of an array using the method `getClass()`) – Erwin Bolwidt Nov 03 '17 at 13:41
  • 2
    @JuanCarlosMendoza I do not think it is a duplicate. I do not question that it is this way, but I wonder why. – Michael Dorner Nov 03 '17 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. – Michael Dorner Nov 03 '17 at 13:47
  • "Objects are instances of things that hold a number of other things together." This vague definition is shared by both classes and arrays. A class holds together a number of fields, while an array holds together a number of elements. Thus, they have something in common, and there is not really a special treatment for arrays. – Klitos Kyriacou Nov 03 '17 at 13:48
  • @MichaelDorner probably best to ask designers of Java if you want to know their reasons... we can only guess – user85421 Nov 03 '17 at 13:59
  • I would not try to find another definition than the one provided in the JLS. It's like asking why Jesus was named Jesus... – AxelH Nov 03 '17 at 13:59
  • When you write "array is a class instance", then I would ask: which class? There is no such accessible class and no JavaDoc to read. – Tom Nov 03 '17 at 14:01
  • @MichaelDorner They said it because it follows from the definitions of a class and an array. A class is something you define in your source code as "class X" (in the first version of Java, now there are a few other ways to declare classes too) - as opposed to arrays, interfaces and primitive types. An array is a higher-order type - you can make an array of any other type in Java (except `void`). It's not something you declare with `public class int[] { ...}` – Erwin Bolwidt Nov 03 '17 at 14:01
  • @AxelH Because the fantasy author of "The bible" thought that's a cool name for the protagonist. – Tom Nov 03 '17 at 14:02
  • This is really a question about English usage not the Java language. – Stephen C Nov 03 '17 at 14:23

1 Answers1

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.

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • I dont think copy paste the same 4 lines from the JLS is really an answer... can you improve ? – vikingsteve Nov 03 '17 at 13:45
  • @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. – biziclop Nov 03 '17 at 13:46
  • @MichaelDorner Because they're created different. How else would you define them and their different modes of creation? – biziclop Nov 03 '17 at 13:48
  • 1
    Just to show that there *is* an alternative, C# arrays are actually instances of class Array. – Klitos Kyriacou Nov 03 '17 at 13:56
  • @KlitosKyriacou I don't really understand how that is relevant here. In the JLS `class instance` is defined as `something that can be created by a class instance creation exception` and that's all there is to it. – biziclop Nov 03 '17 at 13:58
  • You mean, of course, a class instance creation *expression*, although a "class instance creation exception" would also create a class instance: an instance of class Exception! Which proves that there are other ways to create a class instance than just by a "class instance creation expression". The JLS doesn't imply that a class instance creation expression is the *only* way to create an instance. A third way to create them is by using class Class. The relevance of my comment was in answer to the question "How else would you define them". Arrays really can be class instances (just not in Java). – Klitos Kyriacou Nov 03 '17 at 14:04
  • 1
    @biziclop In the JLS, as class instance is defined as an instance of a class. Classes are defined in [Section 8 of the JLS](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html). Interfaces are in Section 9, and Arrays in Section 10. Neither interfaces nor arrays are classes. – Erwin Bolwidt Nov 04 '17 at 05:39