当前位置: 代码迷 >> Java相关 >> 关于String类问题?(已解决但是String新问题出来了)
  详细解决方案

关于String类问题?(已解决但是String新问题出来了)

热度:352   发布时间:2008-04-05 15:17:44.0
关于String类问题?(已解决但是String新问题出来了)
String str=new String("aaa");
str="bbb";

这个有几个对象,产生了几个垃圾内存  我自己觉得是三个对象,两个垃圾内存。但下面的程序有不理解了
代码如下:

class StringTest{

    public void finalize(){
        System.out.print("清理垃圾。");
    }
    public static void main(String[] args){
        StringTest a=new StringTest();
        String str1="123";
        String str2=new String("123");
        str1="2";

        new StringTest();//测试finalize()是可以被调用的。
        System.gc();
    }
}
但为什么finalize()只被调用一次呢

[[it] 本帖最后由 he20041987 于 2008-4-6 11:04 编辑 [/it]]
搜索更多相关的解决方案: String  内存  new  finalize  

----------------解决方案--------------------------------------------------------
class StringTest{

    public void finalize(){
        System.out.print("清理垃圾。");
    }
    public static void main(String[] args){
        new StringTest();
        String str1="123";
        String str2=new String("123");
        str1="2";

        new StringTest();
        System.gc();
    }
}
//这样就有2个了
/*
public static void gc()
Runs the garbage collector.
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

The call System.gc() is effectively equivalent to the call:

Runtime.getRuntime().gc()

protected void finalize()
                 throws Throwable
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
*/

[[it] 本帖最后由 sunkaidong 于 2008-4-5 16:19 编辑 [/it]]
----------------解决方案--------------------------------------------------------
不是这样啊 我问的是String这个类
String str1 = new String("abc"); //jvm 在堆上创建一个String对象   
   
//jvm 在strings pool中找不到值为“abc”的字符串,因此   
//在堆上创建一个String对象,并将该对象的引用加入至strings pool中   
//此时堆上有两个String对象
----------------解决方案--------------------------------------------------------
每个类要有自己的finalize().只有这样的时候当声明没被引用的时候System.gc();才会找到相应的finalize(),做最后的处理
import java.lang.*;
class string{
        private String s;
        public string(String s1){
            s=new String(s1);
            }
        public void finalize(){
        System.out.print("清理垃圾"+s);
    }
    }
class StringTest{

    public void finalize(){
        System.out.print("清理垃圾。");
    }
    public static void main(String[] args){
        new StringTest();
        string str1=new string("123");
        string str2=new string("123");
        str1=new string("1234");

        new StringTest();
        System.gc();
    }
}

[[it] 本帖最后由 sunkaidong 于 2008-4-5 17:26 编辑 [/it]]
----------------解决方案--------------------------------------------------------
还是不太懂! 能不能在我写的那个类中改?这样改了好像意义不一样啊
----------------解决方案--------------------------------------------------------
不好弄String 是final 是不好继承的..它自己没有finalize()所以没办法在清理的时候调用finalize().其实已经能说明问题了
----------------解决方案--------------------------------------------------------
哦 这样啊! 谢谢了
----------------解决方案--------------------------------------------------------
大佬,解释的时候能不用英文不?
----------------解决方案--------------------------------------------------------
强 太强了!
----------------解决方案--------------------------------------------------------
  相关解决方案