Suppose int[] a = new int[]; It will create a new object or not ? and one more question . if int[] a = { 1 , 4 , 5 , 6 } Here it will create a new object or not ? if it's creating a >new object then how it will be created

- 21
- 2
-
Hard to say, since you don't say what language you're using. (The JVM supports several languages.) – Hot Licks Aug 16 '14 at 16:48
-
1Why don't you try running `int[] a = new int[];` and seeing what happens. `int[] a = {1, 2, 4, 5, 6};` will make an array of integers – asimes Aug 16 '14 at 16:49
-
But just in case you happen to be dealing with Java: Yes, all arrays are objects, and they have a (system-created) Class just like any other object. – Hot Licks Aug 16 '14 at 16:49
3 Answers
If you are talking about Java then the answer is Yes it will create a new object
The Java Language Specification section 4.3.1 clearly says that:
An object is a class instance or an array.
Also,
In the Java programming language arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

- 168,305
- 31
- 280
- 331
Arrays are object only. Arrays is the object that holds references of primitive or object

- 1,983
- 3
- 14
- 26
-
Actually, an array is an object that holds either primitives or references to objects. In Java there is no such thing as a reference to a primitive. – Hot Licks Aug 16 '14 at 16:50
Java Arrays are Object(s) with syntatical sugar.
int [] a = new int[2];
a[0] = 1;
a[1] = 2;
System.out.println(Arrays.toString(a));
You can also declare the array
int [] a = {1,2};
Regardless of how you declare the array, it is an Object. Is has a length field, additionally you can cast it to an Object,
int[] a = { 1, 2 };
Object o = a;
System.out.println(o.getClass().getName());
It does output the unfortunately named,
[I
You can use the Array
utility from the reflect package,
System.out.println(Array.get(a, 0));
From the Array
Javadoc,
The
Array
class provides static methods to dynamically create and access Java arrays.

- 198,278
- 20
- 158
- 249