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?

7
  • 1
    Of course, this is just sugar syntax from java ... Commented Nov 9, 2018 at 14:39
  • Why do you think that an array is an object? Commented Nov 9, 2018 at 14:41
  • Arrays, technically speaking, are considered data structures. Commented Nov 9, 2018 at 14:44
  • @NicoHaase well every time we use the new keyword it is to create an instance of some class, right? Commented Nov 9, 2018 at 14:47
  • 1
    Arrays are indeed objects, according to the JLS. Commented Nov 9, 2018 at 14:53

2 Answers 2

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.

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

2 Comments

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?,
@MrakVladar You can assign the reference to a new array, however, while the reference is null, there is no array to add to.
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.

4 Comments

but will it still be an object as Im not creating an instance like we do with the new keyword
so in this case when we use the "new" keyword what are we exactly telling the compiler
You tell the compiler to create a new array object of your concrete type and allocate space for it.
@MrakVladar strange, you are right - stackoverflow.com/questions/8781022/… has the same question and Java seems to have a special definition of "object" ;)

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.