Java中数组的初始化
Java中的数组是在堆中分配内存的,并且数组大小一旦确定就不可以更改。虽然数组本身是在堆中分配内存的,但是数组引用却是在栈上分配内存的。
public class ArrayInit {
public static void main(String[] args) {
int[] a1 = {1,2,3,4,5}; //相当于使用new
int[] a2 = new int[]{1,2,3,4,5};
int[] b1=a1; //相当于拷贝引用,修改b1会影响a1
//对于非基本类型的数组,实际上保存的是其中对象的引用
Integer[] a = {1,2,3,4,5};
Integer[] b = new Integer[]{1, 2, 3, 4, 5};
//传递参数
Other.main(new String[]{"fiddle","de","dun"});
}
}
class Other {
public static void main(String[] args) {
for(String s:args)
System.out.print(s+" ");
}
}