Java Array Vs. Scala Array Which is Easy

An array is a fixed-size box with elements. The index of elements starts from ‘0’. The last element of an array is -1 of the length of an array.

Arrays are most common in modern languages like Java and Scala. Here is the difference in declaring an array.

How to declare an Array in Java

You declare an array type in Java by appending [] to the type. For example, an array of ints is defined as int[].

1   int[] vampireAges = new  int[10]; // ten vampires

Setting and accessing the values in an array uses the same square bracket syntax, such as the following:

1   vampireAges[0] = 1565; // set age of first vampire
2 int age = vampireAges[0] // get age of first vampire

As you can see, the first index of an array is zero. Things tend to start at zero when programming; try to remember this.

Java Vs. COBOL Arrays

Here you will know the syntax to define arrays in Java and COBOL.

How to Declare an Array in Scala

In Scala, you can define an Array like one of the following:

1   var names = new Array[String](2) // size of 2 without values
2 var names = Array("Dracula", "Edward") // size of 2 with values

Related Posts

  • How to Use Filter() on List in Python

  • JCL MSGCLASS Vs. MSGLEVEL Top Differences

  • 20 Software Engineer Gift Ideas: From Gadgets to Games

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.