s*******e 发帖数: 174 | 1 【 以下文字转载自 Java 讨论区 】
发信人: shrubRose (喵喵喵), 信区: Java
标 题: 一道 JAVA Stack vs Heap 题
发信站: BBS 未名空间站 (Mon Nov 9 13:59:15 2009, 美东)
String s1 = "grapefruit";
String s2 = "grapefruit";
请问 s1 and s2 是在 stack 还是 heap 上呢? Does s1 and s2 point to same
address?
String s3 = "grape"+"fruit";
Does s3 point to same address as s1 and s2?
String s4 = new String("grapefruit");
String s5 = new String("grapefruit");
s4 and s5 should be in heap, s4 and s5 should point to different addresses,
right?
System.out.print | c*****o 发帖数: 178 | 2 在heap上。s1,s2是指向同一个address。都是指向常量"grapefruit"
s3也是指向"grapefruit",因为在编译阶段"grape"和"fruit"被连接起来,所以还是指
向同一个常量。
s4和s5指向不同的地址,因为new创建了2个不同的对象,尽管内容相同。
最后一个问题我认为是第一个快一些,不确定。 | s*******e 发帖数: 174 | 3 和我想的一样。。。
【在 c*****o 的大作中提到】 : 在heap上。s1,s2是指向同一个address。都是指向常量"grapefruit" : s3也是指向"grapefruit",因为在编译阶段"grape"和"fruit"被连接起来,所以还是指 : 向同一个常量。 : s4和s5指向不同的地址,因为new创建了2个不同的对象,尽管内容相同。 : 最后一个问题我认为是第一个快一些,不确定。
|
|