0

I'm trying to understand how arrays work in Java. I looked at the source, and the constructor is made private. How does it work then that if I write something like:

String [] myArray = new String [10];

It knows that this is an array, simply by putting the brackets [ ]?

Furthermore, why did they make it so it isn't instantiated by something like how ArrayList are created?

ArrayList<String> myArray = new ArrayList<String>(10);

Is an array not an object? What makes it different?

3
  • 1
    The first one is Java array. The second one is generic collection. Array is there (probably since the beginning), but generic is only available from Java 1.5 Commented Jan 7, 2013 at 6:38
  • 4
    Where exactly did you find a private constructor? Commented Jan 7, 2013 at 6:43
  • 2
    I don't think the OP is asking about the semantic difference between array and ArrayList. I think this is more of a question about syntax and possibly how the bytecode differs. Commented Jan 7, 2013 at 6:43

5 Answers 5

4

It knows that this is an array, simply by putting the brackets []?

Yes, that's how it works. This is a special syntax for declaring and creating arrays (and accessing their elements).

The language (and the JVM) has special support for arrays (since they are frequently used). In addition to the brackets you found, there is also a special for-each-loop, a syntax to create array literals, and arrays can be used for vararg method parameters.

Is an array not an object?

Arrays in Java are Objects (even arrays of primitive types).

What makes it different?

There is not much that is not different...

Except that you can assign arrays to a variable of type Object (and that they can be null) there is not too much object-like in them. For example, while they have methods such as equals and toString, their implementations probably do not do what you expect (see the Arrays class for more useful versions). You also cannot subclass the internal "classes" that make arrays work.

To sum up, arrays are objects, but special objects. Unlike ArrayList, which is built just like any other class. You could roll your own Collection in Java, but to build your own array, you need to build your own JVM.

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

3 Comments

Just to add: array has special instruction to create and access them in the JVM, while generic collections will invoke method normally like any other classes. en.wikipedia.org/wiki/Java_bytecode_instruction_listings
The OP seems to know that an array is an Object. I believe that he wonders why you can't create an array through a 'regular' constructor and maybe how the "[]" is desugarized (salted ?) by java.
Well, that's how it is... At least you still get the new keyword.
0

Arrays are fixed size (need to declare size in creation time of Object), so in runtime we can't increase the size of Array.

While ArrayList are dynamically growing in runtime, even if we declare the Size at creatin time.

Comments

0

An array is an object, however arrays can also be of primitives, but a primitive array is still an Object.

Furthermore, why did they make it so it isn't instantiated by something like how ArrayList are created?

ArrayLists use generics to allow an extending type, and generics can't be used for primitives. Not to mention, generics came in Java 5 whereas arrays have always been here.

Comments

0

Java arrays (noted with []) are special kind of objects. Much like int and friends. ArrayLists and such are fully featured classes. There are many differences and you will find classes that encapsulate these types so that you can manipulate them like normal classes. Most notably you can not subclass a type like array or int. But you will find ArrayList or Integer if you want to treat them object oriented.

Also to be noted parametrized types, the ArrayList<String> notation, is a relatively recent addition to Java (I think it appeared in Java 1.5 but I may be mistaken). Before that, collections would contain bare Object that you had to cast back into the right class.

1 Comment

I don't think array is classified as primitive type.
0

It knows that this is an array, simply by putting the brackets [ ]?

Yes.

Furthermore, why did they make it so it isn't instantiated by something like how ArrayList are created?

Because (a) they didn't and (b) why should they? If you want to know the answer you're in the wrong place. Ask Jim Gosling. There are plenty of other languages where this is done the same way; why are you singling out Java for your special attention?

Is an array not an object?

Yes.

What makes it different?

The fact that the [] syntax is treated specially? The fact that there are an infinite number of array types: (one for each primitive type plus one for each possible class) times (any number of dimensions from 1 to Integer.MAX_INT).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.