字符串数组语法

时间:2018-11-06 01:14:30

标签: java arrays

以以下代码片段为例:

String[] s = new String[2];
s[0] = "Alex";
s[1] = "John";
String[] t = {"Michael", "Megan", "Chelsea"};
s = t;
System.out.println(s.length);

运行时,为什么要打印出3而不是给出编译或运行时错误?

1 个答案:

答案 0 :(得分:0)

为什么应该出现编译时或运行时错误?

使用 s = t ,您将 s 指向 t 指向的同一数组,换句话说,它们都指向相同的内存位置存储数组的位置。