Javascript vs. Java: Arrays

Despite the similarities in their name, Javascript and Java are two entirely different programming languages. Certainly, there are some similarities; after all, the basic logic that goes into coding is the same regardless of which language it’s being typed in. However, oftentimes these similarities make the places where the two languages differ even more confusing. As an experienced Javascript programmer who recently began learning Java, one of the first big differences that tripped me up was in the two languages’ interpretations of the array data type.

Common Ground

In all languages, an array is a data type that consists of multiple values collected in a single variable.

["a", "b", "c", "d", "e"]

Above is an array written in Javascript, and below is an array written in Java.

{"a", "b", "c", "d", "e"}

Aside from swapping out the square brackets for curly braces, it’s the same thing, right? Each individual value in an array is called an element, and each element has an index — an integer that denotes the element’s place in the array. Indices start at 0 and increase by one for every element in the array. In the example above, a’s index is 0, b’s is 1, c’s is 2, and so on.

Imagine that the variable thisArray has been assigned this array as its value. If we wanted to get the value “a” specifically, we would write it as thisArray[0] — the name of the variable followed by the index of the desired element in brackets. This notation is the same across languages. Also constant is the length property: typing thisArray.length will return the number of elements in the array. Unfortunately, this is essentially where the similarities end.

Declaring Arrays

Since declaring variables in general is something that varies from Javascript to Java, this shouldn’t be too surprising, but it’s worth pointing out: declaring an array in Java requires more information than declaring one in Javascript.

let thisArray = ["hello", "world"];

To declare an array in Javascript, you really only need to provide the name of the array. Regardless of the data type of the elements, the syntax for declaring an array is always the same. However, in Java…

String[] thisArray = ["hello", "world"];

The data type of the elements in the array — in this case, strings — also needs to be provided. If I were to use the same syntax to declare an array of integers…

String[] thatArray = {1, 2, 3};

Java would generate an error, because the elements in thatArray are not strings. It’s noteworthy that an array of mixed data types can be created in Java by defining the array as an array of objects, e.g.

Object[] theOtherArray = {"string", 1, 2.33, true};

This does not, however, work with arrays of arrays — to do that, two sets of square brackets must be present in the variable type declaration.

Object[][] twoArraysInOne = { {"string", 1}, {2.33, true} };

Morphing Arrays

Javascript arrays are very easily changed. There are a whole host of array methods that exist to allow for easy manipulation of arrays. Push(), for instance, adds a new element to the end of any given array. Below is an example.

let thisArray = ["hello", "world"];
thisArray.push("!");
console.log(thisArray);
//logs ["hello", "world", "!"]

Java arrays are a bit more restricted in what they can change. Once an array has been declared, the length of the array cannot be changed. It is therefore impossible to add or remove elements directly. Individual elements can, however, be replaced.

String[] thisArray = {"hello", "world"};
thisArray[0] = "goodbye";
System.out.println(thisArray[0] + " " + thisArray[1]);
//prints "goodbye world"

Printing Arrays

The example above also illustrates another particular of Java arrays, which is that the contents of an array cannot be printed simply by calling the array.

System.out.println(thisArray);
//this will not print "goodbye world"!
//it prints something like "[Ljava.lang.String;@182decdb" instead

What will be printed instead is a reference to the space allocated to storing the array, which is unlikely to be of use to the coder or the user. If you want to print the contents of an array, calling on each element individually is your best bet. To save time on longer arrays, building a for loop to do so might help. Here’s an example:

for (int i = 0; i < thisArray.length; i++) {
System.out.print(thisArray[i]);
}

Meanwhile, Javascript logs any arrays to the console exactly as written in the code itself, complete with square brackets.

ArrayLists

What if a Java developer wants an array that functions more like Javascript arrays, with the ability to add, remove, and easily sort elements? Well, Java has a data type called an ArrayList — one that is absent from JavaScript. In Java, ArrayLists can do all of the above, though the syntax used to define and manipulate them differs from regular arrays.

ArrayList<String> thisArrayList = new ArrayList<String>();
thisArrayList.add("hello");
thisArrayList.add("world");
//thisArrayList now has two elements: "hello" and "world"

If you want an array with more mutability than standard Java arrays, then an ArrayList might be your best bet — just make sure to remember that the wording used to work with them might change. For instance, the number of indices in an ArrayList is accessed with the size() method, not the length attribute.

To read more about arrays and ArrayLists in Java, I suggest the w3schools Java tutorial here. A similar tutorial exists for Javascript here. This article presents the basics of arrays in both languages, but there is much more to learn and master!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store