为以下内容创建了多少个字符串对象?

时间:2014-01-22 07:39:19

标签: java string

String summer = new String("Summer");
String summer2 = "Summer";
System.out.println("Summer");
System.out.println("autumn");
System.out.println("autumn" == "summer");
String autumn = new String("Summer");

我的答案是2 + 0 + 0 + 1 + 1 + 1 = 5。这是对的吗?

3 个答案:

答案 0 :(得分:5)

对我来说很合理。

String summer = new String("Summer");

创建了2个实例。第一个是文字"Summer"。第二个是因为构造函数被调用。

String summer2 = "Summer";

0个实例:这只是一个引用赋值。

System.out.println("Summer");

0个实例。文字"Summer"取自缓存。

System.out.println("autumn");

1个实例"autumn"

System.out.println("autumn" == "summer");

1个实例"summer"

String autumn = new String("Summer");

由构造函数调用引起的1个实例。文字"Summer"取自缓存。

底线:2 + 0 + 0 + 1 + 1 + 1 = 5

答案 1 :(得分:1)

是的,这是正确的。总之,将创建5个对象。

String summer = new String("Summer"); // 2 objects. One for constructor and other for literal
String summer2 = "Summer";  // 0. Will point to the literal already created.
System.out.println("Summer"); // 0. Same as above statement
System.out.println("autumn"); // 1 object will be created
System.out.println("autumn" == "summer"); // 1 object. 'Summer' and 'summer' are not same
String autumn = new String("Summer"); // 1 object. new String() will cause a new object creation but not the literal

答案 2 :(得分:1)

编译并计算:

import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class test {
    private static Unsafe unsafe;

    static {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            unsafe = (Unsafe) field.get(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static long addressOf(Object o) throws Exception {
        Object[] array = new Object[]{o};

        long baseOffset = unsafe.arrayBaseOffset(Object[].class);
        int addressSize = unsafe.addressSize();
        long objectAddress;
        switch (addressSize) {
            case 4:
                objectAddress = unsafe.getInt(array, baseOffset);
                break;
            case 8:
                objectAddress = unsafe.getLong(array, baseOffset);
                break;
            default:
                throw new Error("unsupported address size: " + addressSize);
        }

        return (objectAddress);
    }

    public static void main(String... args)
            throws Exception {

        String summer = new String("Summer");
        System.out.println("Addess: " + addressOf(summer));
        String summer2 = "Summer";
        System.out.println("Addess: " + addressOf(summer2));
        System.out.println("Addess: " + addressOf("Summer"));
        System.out.println("Addess: " + addressOf("autumn"));
        System.out.println("Addess: " + addressOf("autumn"));
        System.out.println("Addess: " + addressOf("summer"));
        //System.out.println("Summer");
        //System.out.println("autumn");
        //System.out.println("autumn" == "summer");
        String autumn = new String("Summer");
        System.out.println("Addess: " + addressOf(autumn));


    }

    public static void printBytes(long objectAddress, int num) {
        for (long i = 0; i < num; i++) {
            int cur = unsafe.getByte(objectAddress + i);
            System.out.print((char) cur);
        }
        System.out.println();
    }
}