2

I'm New to Java, I was reading up on how to use array In java. It said to use array in java you have to follow three steps which are Declaration of array, Creating Array and Initialising Array, And I get it But how does the following code work when I haven't followed all of the steps above

int[] array = {1234,234,43,15234,433}

Here I have declared the type of variable I'll be using and named it and directly used it. Unlike the method we usually use In OOP like

int[] array = new int[10];

Does the first way still make array an Object? If yes how?

Mrak Vladar
  • 598
  • 4
  • 18

2 Answers2

2

The first way is just a shorthand for

int[] array = new int[5];
array[0] = 1234;
array[1] = 234;
array[2] = 43;
array[3] = 15234;
array[4] = 433;

Since the following option

int[] array = {1234,234,43,15234,433}

is a lot shorter you can use it whenever you already know the elements of the array at compile time. Please note that in your second case you only created the array, but did not actually fill it with elements.

An array is considered as an object as of the Java Language Specification, Chapter 4.3.1 Object.

Glains
  • 2,773
  • 3
  • 16
  • 30
  • but will it still be an object as Im not creating an instance like we do with the new keyword – Mrak Vladar Nov 09 '18 at 14:42
  • so in this case when we use the "new" keyword what are we exactly telling the compiler – Mrak Vladar Nov 09 '18 at 14:46
  • You tell the compiler to create a new array object of your concrete type and allocate space for it. – Glains Nov 09 '18 at 14:49
  • 1
    @MrakVladar strange, you are right - https://stackoverflow.com/questions/8781022/is-an-array-an-object-in-java has the same question and Java seems to have a special definition of "object" ;) – Nico Haase Nov 09 '18 at 14:49
2

but will it still be an object as Im not creating an instance like we do with the new keyword

There is no way to prevent creating a new Object unless you explicitly initialise an existing array.

int[] array = {1234,234,43,15234,433}; // creates a new array object every time

is shorthand for

int[] array = new int[] {1234,234,43,15234,433};  // creates a new array object every time

The only way to prevent using a new object is either

int[] array = null; // no new object

or

int[] array = reusedArray; // no new array
array[0] = 1234;
array[1] = 234;
array[2] = 43;
array[3] = 15234;
array[4] = 433;

when we use the "new" keyword what are we exactly telling the compiler

Create a new object on the heap (unless escape analysis can eliminate the object creation) While the Oracle/OpenJDK version 6 to 11 can place some objects on the stack instead of the heap to reduce heap usage, this doesn't apply to arrays AFAIK.

[Added] Is an array an object?

Variables in Java are only primitives or references. If it's not a scalar primitive, it's an object. e.g. Boolean, int[], String, Enum variables are all references to Objects. i.e. String s is not an object.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • what is explicitly initialising an existing array, can you give me an example, and when I do something like this int[] array = null , what if later I try to add something in that array will it work?, – Mrak Vladar Nov 09 '18 at 14:57
  • @MrakVladar You can assign the reference to a new array, however, while the reference is `null`, there is no array to add to. – Peter Lawrey Nov 09 '18 at 15:02