interning strings

Date view Thread view Subject view Author view

From: Godmar Back (gback@cs.utah.edu)
Date: Wed Oct 21 1998 - 18:16:38 EDT


Using a string's finalizer to remove it from the unintern table shows
a behavior that is inconsistent with Sun's behavior and possible inconsistent
with the spec. However, it only seems to matter in obscure circumstances,
as demonstrated by the program below.

It outputs `true' on Sun's impl, `false' under Kaffe.

        - Godmar

--- Intern.java ---
/**
 * test a situation where a string is interned, finalized, reattached in
 * the finalizer, but finalized anyway and no longer interned as a result.
 */
import java.util.Hashtable;

public class Intern
{
    static Hashtable reattach = new Hashtable();
    String s1;
    String s1_intern;

    public void finalize()
    {
        reattach.put(s1, s1_intern);
    }

    public Intern(String s)
    {
        s1 = s;
        s1_intern = s.intern();
    }

    static String getString(int nr) { return "This is string " + nr; }

    static void dummy()
    {
        new Intern(getString(0));
    }

    public static void main(String av[]) throws Exception
    {
        dummy();
        System.gc();
        Runtime.getRuntime().runFinalization();

        String s2_intern = getString(0).intern();
        String s1_saved_intern = (String)reattach.get(getString(0));
        boolean w = (s1_saved_intern == s2_intern);
        System.out.println(w);
    }
}


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Sat Sep 23 2000 - 19:57:01 EDT