Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between non-initialized items in an array in JavaScript vs Java?

Tags:

I was just playing around with JavaScript and got stuck with a simple program.

I declared an array in JavaScript like

var a = [0, 1, 2];

Then as there is no fixed size for an array in JavaScript and we can add more to the array, I added another integer to array.

a[3] = 3;

And as expected If I try to access a[4] I am definitely going to get it as undefined.

Now, if I take an array

var a = [0,1,2];

And add another element

a[4] = 4;

I have intentionally not defined a[3], and this also gives me a[3] as undefined.

Here is a fiddle where this can be observed: http://jsfiddle.net/ZUrvM/

Now, if I try the same thing in Java,

int[] a = new int[4];
a[0] = 0;
a[1] = 1;

a[3] = 3;

Then I end up with

a[2] = 0;

You can see this on ideone: https://ideone.com/WKn6Rf

The reason for this in Java I found is that the four variables are defined while declaring the array and we can only assign values to the declared size of array. But in JavaScript when I declare an array of size 3 and then add 5th element why does it not consider the 4th element to be null or 0 if we have increased the array size beyond 4?

Why do I see this strange behavior in JavaScript, but not in other languages?

like image 498
Naeem Shaikh Avatar asked Jun 04 '14 13:06

Naeem Shaikh


People also ask

What happens if an array element is not initialized in Java?

Even if you do not initialize the array, the Java compiler will not give any error. Normally, when the array is not initialized, the compiler assigns default values to each element of the array according to the data type of the element.

What is the difference between null array and an array of length zero in Java?

You can have a zero-length array -- contains no elements. You can have a null array reference -- the reference is null, meaning that no array actually exists. You can have an array with elements that are all set to null -- this is the default for an array of references when it's initially created.

What happens if an array element is not initialized?

If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. The same applies to elements of arrays with static storage duration. (All file-scope variables and function-scope variables declared with the static keyword have static storage duration.)

Is Java array initialized?

Array Initialization in JavaTo use the array, we can initialize it with the new keyword, followed by the data type of our array, and rectangular brackets containing its size: int[] intArray = new int[10]; This allocates the memory for an array of size 10 . This size is immutable.


2 Answers

Why is this strange behavior in JavaScript?

Because arrays are only objects. If you access a nonexisting property, you get back undefined. You simply didn't assign an element at index 3, so there is nothing.

Auto-growing the array by assigning higher indices does not change this behaviour. It will affect the .length property, yes, but the intermediate indices will stay nonexistent. This is known as a sparse array.

Why is this strange behaviour in Java / C / C++?

Because arrays are chunks of allocated memory, and when allocating an array of size 4, all its elements take their values from that memory location. To avoid indeterminate values, in some languages/occasions the fields get default-initialised, typically with 0.

like image 99
Bergi Avatar answered Oct 16 '22 15:10

Bergi


JavaScript isn't a strongly typed language.

In Java you declared an array of integers. The default value of any element in that array is 0.

In JavaScript, when you declare an array, the default value is undefined as it can hold anything, number, string, any object (including another array). All elements of a JavaScript array don't have to be the same type.

like image 24
Smeegs Avatar answered Oct 16 '22 14:10

Smeegs