CSDN大本营看到得 看到很多人在评论这个题的答案, 自己看了下 感觉也有点问题,大家看看这个题目
阅读下列代码,并回答问题Student[ ] stu =new Student[ 3 ] ;for( int i=0;i<stu.length;i++ ){ stu[ i ]=new Student();}Student temp = stu[ 0 ];stu[ 0 ] = stu[ 1 ];stu[ 1 ] = temp;
请说明此段代码中,共声明了几个引用,共构造了几个对象答案是4个引用,5个对象. 答案对么? 如果对 到底是几个引用和对象
阅读下列代码,并回答问题Student[ ] stu =new Student[ 3 ] ;for( int i=0;i<stu.length;i++ ){ stu[ i ]=new Student();}Student temp = stu[ 0 ];stu[ 0 ] = stu[ 1 ];stu[ 1 ] = temp;
请说明此段代码中,共声明了几个引用,共构造了几个对象答案是4个引用,5个对象. 答案对么? 如果对 到底是几个引用和对象
这体还不如直接考论数组是什么
我认为是2个引用,4个对象
数组是java的四种类型之一(接口,类,原生类型和数组)
程序中的两个引用是Student[ ] stu和Student temp
而像这样的stu[0],stu[1]不是引用,JVM获取数组元素是靠的一个数组引用,即stu和数组的索引,即1,2...
每次去元素都从栈中弹出数组的索引和数组的引用,然后根据这个索引和引用找到给定数组的指定索引位置的值压入栈,当然取元素和存储元素的指令是不一样的,而且存储元素需要从栈中弹出值,索引,和数组引用
所以说stu[0]不是引用,形象的说它就是stu.get(0)方法,当然数组是没有这个方法的
程序中所包含的对象有4个吧,因为new了四次
对象:
new Student[ 3 ] ,3个new Student(),
引用:
stu,stu[0],stu[1],stu[2],temp,
对象 3个Student对象,1个Student数组对象
一个String[]对象,3个Student对象5引用
6个对象,首先Student[ ] stu =new Student[ 3 ] ;创建了3个
stu[ i ]=new Student();
再创建3个
引用有:stu stu[0] stu[1] stu[2] temp
Student[] stu =new Student[3];
只是创建了一个数组对象而已 哪来的三个