为什么这个test1的长度是6?我明明开辟了1024的内存啊
public class AyyayTest {
public static void main(String[] args) {
byte[] test1=new byte[1024];
test1="doload".getBytes();
System.out.println(test1.length);
}
}
为什么这个test1的长度是6?我明明开辟了1024的内存啊
public class AyyayTest {
public static void main(String[] args) {
byte[] test1=new byte[1024];
test1="doload".getBytes();
System.out.println(test1.length);
}
}
"doload"是一个长度为6的字符串,然后你又调用了getBytes(),这个方法的作用是将当前的字符串转换为byte数组,这个转换后的数组长度为6,并且
你将这个数组重新赋值给了test1对象,那么test1对象就指向了这个长度为6的新数组,所以你输出的结果是6,而不是1024。