-2

According to this question (Is an array an object in java), arrays are considered objects in Java.

The answers to that question mainly cite references to the language specification. My question is what methods and fields does that object have? Can I instantiate it directly (int[] myArr = new Array<int>(), or something similar)? In short, how does it work?

A quick search yielded no results. I see the JavaDoc for the Arrays object (used for sorting arrays, etc), but not JavaDoc for an Array.

Edit:

Just to be clear, I am NOT trying to learn to use an array for the first time. I am asking a question about the mechanics of the java language. I know I can create an array:

int[] a = new int[3];

I'm asking if there is a different way to do it - since it is an object, is there an object constructor that works? Is there documentation on the array object?

Community
  • 1
  • 1
nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • What was unclear from the answers in the question you linked? There is a syntax for instantiating arrays directly as it is. – pvg Apr 23 '16 at 07:08
  • see my edit. i'm not asking about how to make an array, i'm asking about language features – nhouser9 Apr 23 '16 at 07:44

3 Answers3

4

Arrays have a public final length attribute. They implement the Cloneable and Serializable interfaces. And of course, they inherit all methods from Object (equals(), hashCode(), wait(), etc.).

There is no class that you can use to construct an instance as you're trying in your question. The syntax to instantiate them is

int[] myArr = new int[someLength];

or, to populate them with non-default values;

int[] myArr = new int[] {1, 2, 3};

You can also use

int[] myArr = {1, 2, 3};

But note that you can't use this short syntax to assign a value to a previously declared variable. I.e. you can do

myArr = new int[] {1, 2, 3};

but not

myArr = {1, 2, 3};
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    The behaviour of an array is written into the language rather than a class +1 – Peter Lawrey Apr 23 '16 at 07:15
  • I know I can do it that way - I'm asking if there is a different way to do it, by calling an object constructor. – nhouser9 Apr 23 '16 at 07:42
  • And my answer is: *There is no class that you can use to construct an instance as you're trying in your question*. Since there is no class, you can't call its constructor. – JB Nizet Apr 23 '16 at 07:51
  • An important distinction is that arrays do have a class. The fact that you can't use it to instantiate arrays is mostly a quirk of the language/runtime rather than a consequence of the absence of such a class. – pvg Apr 23 '16 at 22:21
1

Extract from Bozho answer to a similar question Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?

Arrays are a language feature - they have a specific syntax for declaring and accessing. And their class definition is hidden from you.

They have a representation in the refleciton API - java.lang.reflect.Array

Community
  • 1
  • 1
0

You can declare an array in a similar way as any other Class's object.

If you aren't sure about the size of array at the time of declaration,then you can go like this---

class ArrayExample
{
int arr[];
public static void main()
{
//take length as input from the user
arr=new int[anylength];
//now do whatever you want to do with this array object
}
}
asad_hussain
  • 1,959
  • 1
  • 17
  • 27