Java’s Array

David Shin
3 min readApr 5, 2021
Resources: https://hackernoon.com/java-vs-javascript-heres-what-you-need-to-know-9b1b708394e2

In javascript, an array is a very common object that we use often in our coding. So what is an array in JavaScript? According to the MDN website, “Array class is a global object that is used in the construction of arrays; which are high level, list-like objects.”, in a simple word it’s a container (ex. [ ]) that can store various data in it. Now for those who have some knowledge of JS, know that an array can store pretty much any data such as numbers, string, boolean, and even an array too!

//JavaScript example of Arraylet container = [];container.push(1)
container.push("hello world")
container.push(true)
container.push(["another array"])
console.log(container) //[1, "hello world", true, ["another array"]]

Now at first glance, those who are studies JS first might think that this is how an array works, but in the underhood, the reason why we can store any data types in an array is that it’s the dynamic array in JavaScript. In another word JavaScript is smart enough that it’s will control the memory allocation of an array so that users don’t have to deal with what types of data type to choose for an array.

Ok, that’s very cool, right? now let’s compare with Java. In Java program language, it’s quite different to use an array.

//Java example of Arrayint[] container = new int[5]; //Declare a variable with an array 
that has integer data type and
5 indcies memory space.
container[0] = 10; //Define first index of array with integer 10.
container[1] = 20; //Define first index of array with integer 20.
container[2] = 30; //Define first index of array with integer 30.
container[3] = 100; //Define first index of array with integer 100.
container[4] = "Hello Word"; //Compile Error.

Whoah, what just happened here? Let’s take a look from the beginning. First-line shows that we first declared a variable named “container” of an array with the integer data type. Equal signs show that we create a new array class that has 5 spaces to allocate. In simple words, the new array called “container” has empty elements inside with 5 indices open to be store. Hmm…. ok, kind of makes sense right? It sure does make a lot of difference with JavaScript but the very straightforward language that Java uses for creating an array!

Now before we move to the next line, let’s understand what an array means in Java. According to tutorialspoint.com “Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.” To understand this let’s look a syntax first!

//Syntax in JavadataType[] arrayReferenceVariable;

As example shows, to delcare a variable to reference the array, we must use the right data type of array the varaible can reference. So let’s go back to above Java example of an array. First we declare the variable “container’ that is going to be an integer data, so then it must be store with only integer value! Now if we look at the last line, we try to set up value with string type to an integer type array , so that will cause an compile error.

Quie different right?

--

--