public static void main(String args[])
{
int []num=new int[10];
int i=0;
while(i<10)
{
num[i]=new Integer(i);
i++;
}
}public static void main(String args[])
{
Integer num;
int i=0;
while(i<10)
{
num=new Integer(i);
i++;
}
}
{
int []num=new int[10];
int i=0;
while(i<10)
{
num[i]=new Integer(i);
i++;
}
}public static void main(String args[])
{
Integer num;
int i=0;
while(i<10)
{
num=new Integer(i);
i++;
}
}
一样的
当然了,这个回收不回收还得看JVM的心情。
因为在第一个程序结束前,在它的对内存有11个对象,一个int型的数组对象,和10个int型数组指向的Integer对象。
而第二个就是分别每次产生一个Integer对象,虽然产生了10次,但是每产生一次就又把引用指向了下一个产生的对象,此时上一个Integer对象因为没有引用指向它而被java的垃圾自动收集机制所清除。所以在第二个程序结束前,在堆内存中只有一个Integer对象。
由以上分析可知第一个占内存。
希望我的分析能对你有帮助。