Home >Web Front-end >Front-end Q&A >What are the differences between java arrays and javascript arrays?
Difference: 1. The length of an array must be specified in Java and cannot be changed; while the length of a JavaScript array is variable and there is no mandatory requirement to define the length. 2. The data types in Java arrays must be consistent; JavaScript arrays can store any data type.
The operating environment of this tutorial: windows7 system, javascript1.8.5&&java8 version, Dell G3 computer.
The difference between java array and javascript array
The length must be specified when defining an array in java, and the length cannot be changed after it is determined. Arrays in js are similar to collections in java. The length is variable and there is no mandatory requirement to define the length.
js array can be automatically expanded, and the array will not cross the boundary
JS array can store any data type
Once the length of a java array is defined, it cannot be changed
The data types in the java array must be consistent
javadefine array
int a[] = new int[3];//定义一个新数组,在内存中分配了3*4个字节 //快速初始化数组的两种方式 int b[] = {1,2,3}; int c[] = new int[]{1,2,3};
javascriptdefine array
There are four ways in total
/* 数组: 1、数组内可以存放任意类型的数据 2、数组元素不赋值,则为undefined 3、打印数组时,如果某个元素没有赋值,则为“” 4、访问数组范围以外的元素时,不会出现越界异常,为undefined 5、定义的数组大小,依然可以添加更多的元素 */ var a = new Array(); alert(a[5]);//undefined,不会出现下标越界 var b = new Array(5); //规定数组的长度为5 b[7] = "ccc"; alert(b[7]);//ccc,定义数组的长度没什么卵用 var c = new Array("tom","lily","lucy"); var d = ["tom","lily","lucy"];//推荐使用
[Recommended learning: javascript advanced Tutorial】
The above is the detailed content of What are the differences between java arrays and javascript arrays?. For more information, please follow other related articles on the PHP Chinese website!