Different handles arrays

In C / C++

Despite the differences between C and C++, they are similar in their treatment of arrays. In most cases, an array name is equivalent to a pointer constant to the first element of the array. This means that you can’t initialize the elements of one array with another array using a simple assignment.

For example,

arrayA = arrayB; /* Compile error: arrayA is not an lvalue */ 

is interpreted as an attempt to make arrayA refer to the same area of memory as arrayB. If arrayA has been declared as an array, this causes a compile error because you can’t change the memory location to which arrayA refers. To copy arrayB into arrayA, you must write a loop that does an element-by-element assignment or use a library function such as memcpy that does the copying for you (usually much more efficiently).
In C and C++, the compiler tracks only the location of arrays, not their size. The programmer is responsible for tracking array sizes, and there is no bounds checking on array accesses — the language won’t
complain if you store something in the 20th element of a 10-element array. As you can imagine, writing outside the bounds of an array usually overwrites some other data structure, leading to all manner of curious and difficult-to-find bugs. Development tools are available to help programmers identify out-of-bounds array accesses and other memory-related problems in their C and C++ programs.

In Java

Unlike a C array, a Java array is an object in and of itself, separate from the data type it holds. A reference to an array is therefore not interchangeable with a reference to an element of the array.
Java arrays are static, and the language tracks the size of each array, which you can access via the implicit
length data member. As in C, you cannot copy arrays with a simple assignment: If two array references have the same type, assignment of one to the other is allowed, but it results in both symbols referring to the same array, as shown in the following example:

byte[] arrayA = new byte[10];
byte[] arrayB = new byte[10];
arrayA = arrayB; // arrayA now refers to the same array as arrayB

If you want to copy the contents of one array to another, you must do it element by element in a loop or call a system function:

if( arrayA.length <= arrayB.length ){
        System.arraycopy( arrayA, 0, arrayB, 0, array.length );

Each access to an array index is checked against the current size of the array, and an exception is thrown if the index is out of bounds. This can make array access a relatively expensive operation when compared to C or C++ arrays; although, in cases in which the JVM can prove that the bounds check is unnecessary, it is skipped to improve performance.
When arrays are allocated, the elements are initialized to their default values. Because the default value for object types is
null for object types, no objects are constructed when you create an array of objects. You must construct the objects and assign them to the elements of the array

Button myButtons[] = new Button[3]; // Buttons not yet constructed
for (int i = 0; i < 3; i++) {
     myButtons[i] = new Button(); // Constructing Buttons
// All Buttons constructed

or use array initialization syntax (which is allowed only where the array is declared):

Button myButtons[] = {new Button(), new Button(), new Button()};

In C#

C# arrays are similar to Java arrays, but there are some differences. The Java concept of a multidimensional array – an array of array objects such as int[2][3] — is called a jagged array in C#, and multidimensional arrays are specified using comma-separated arguments, as in int[2,3]. Arrays can be declared to be read-only. All arrays also derive from the System.array abstract base class, which defines a number of useful methods for array manipulation.

In JavaScript

Arrays in JavaScript are instances of the Array object. JavaScript arrays are dynamic and resize themselves automatically:

Array cities = new Array(); // zero length array
cities[0] = "New York";
cities[1] = "Los Angeles"; // now array is length 2

You can change the size of an array simply by modifying its length property:

cities.length = 1; // drop Los Angeles...
cities[ cities.length ] = "San Francisco"; // new cities[1] value

You can use methods on the Array object to split, combine, and sort arrays.

(Follow Programming Interview)

Leave your thoughts