答案是两个,为什么啊?
解决方案 »
- 如果输入的日期大于12月8日,则执行{...}语句,用java SE怎么表达
- 形如 public void aa(int... p) 的语句中,int... p 是什么意思?
- 小弟最近做了一个银行的简单查询,存钱,取钱的系统,有点问题,请求帮助
- 怎么样向数据库中插入图片
- java能否获得操作系统的文件目录
- 求一简单java游戏TicTacToe制作思路,急!
- 如何配置Tomcat以连接sql server??
- JAVA编程思想
- 请问高手,gif/jpeg文件的格式是什么样的,我想用他们来传输数据
- 请问那里有关于虚拟机实现的源代码?谢谢
- Java如何调用C++写的dll?
- 讨论一段关于java内存管理的代码
创建了1个引用1个对象,不是2个对象!
相当于
String s;//引用
s = new String("xyz");//用NEW创建的叫对象,引用指向对象了!
String s = new String("xyz");和String s = "xyz";有区别吗?
String s = "xyz"只创建了一个对象,而String s = new String("xyz")则创建了一个引用和一个对象。
如果是普通类的话是一个对象一个引用,不过String类好像有点特殊。
String s = new String("xyz");和String s = "xyz"肯定是有区别的。
顶一把,高手来解释一下阿。
String(String original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.这样就很清楚了"xyz"作为参数是一个String对象,而s是它的一个copy,第二个String对象,所以答案是两个String对象。
btw看文档还是很有用的。
"xyz"本身作为字符常量,在汇编语言中应该作为常量放在数据段,Java有一个类似数据段的constant pool保存这个常量,在classloader加载这个类的时候就把"xyz"和这个类的其他一些信息放在constant pool
new String("xyz")根据常量"xyz"在heap上创建String对象
s只不过是stack上的一个引用,指向heap上的String对象
所以,一共两个对象
所以,是一个引用,一个对象
楼上,这个问题在 hsnh1981(黑色男孩) 其实已经回答了。
String s = "xyz"只创建了一个对象,而String s = new String("xyz")则创建了一个引用和一个对象。String s = "xyz"这句代码中的s就不是引用了?你的回答完全不贴边.
还有,一个引用和一个对象也算是两个对象?引用怎么可能也是一个对象,这种说法就更说不过去了.
绿色毒汁的解释也有点问题,没说清楚是哪两个对象,heap上的肯定是一个对象,另一个是哪个?在constant pool中?
还有,JAVA编程思想中也明确说过,对于每个字符串常量,编译器会将其自动生成一个String对象.至于这个自动生成的对象是存放在哪里的则没有说.
因此还有另一种可能的正确答案,就是String s = "xyz"和String s = new String("xyz")是完全相同的,写法不同而已.
1."xyz"--常量,存在于常量内存区
2.new String("xyz")--变量,存在于堆栈中
在执行
String s = new String("xyz");之前有String s1 = "xyz"或String s1 = new String("xzy")呢那
String s = new String("xyz");还是两个吗?
一般来说heap中的一块特殊区域保存着method area,method区类似汇编的代码段,包含了方法(函数)和constant pool和其他信息等
constant pool内又包含了编译器词法/语法分析阶段找到和预计算出来的所有常量,不光是primitive numeric literal比如,还包含String literal. 还有类的方法属性的引用名等。>>绿色毒汁的解释也有点问题,没说清楚是哪两个对象,heap上的肯定是一个对象,另一个是哪个?在constant pool中?
我说了阿,当然是literal在constant pool,new 在 heap阿
String s1 = "test";//编译期放入constant pool
String s2 = new String("test");
System.out.println(s1 == "test");
System.out.println(s2 == "test");
System.out.println(s1 == s2);
s2 = s1.intern();//把constant pool内的"test"给s2
System.out.println(s1 == "test");
System.out.println(s2 == "test");
System.out.println(s1 == s2);
s1 == "test" true
s2 == "test" false
s1 == s2 false
s1 == "test" true
s2 == "test" true
s1 == s2 true
对s2 == "test" false感到吃惊。我水平太菜了
String s = "Test";
The above construct is equivalent to, but more efficient than, this one, which ends up creating two Strings instead of one:
String s = new String("Test");
The compiler creates the first string when it encounters the literal string "Test", and the second one when it encounters new String.
source: http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.htmlAdvice: If you are C/C++ programmer be careful with shortcut assignment operator "+=" in Java when you work with Strings!Why: The shortcut assignment operator += when used with Strings may confuse C and C++ programmers at first. Recall that a += b is equivalent to a = a + b. Let's look at two codesamples written in C++ and the Java programming language:
//C++ code
string* s1 = new string("hello");
string* s2 = s1;
(*s1) += " world";
cout<<*s1<<endl<<*s2<<endl;
return 0;
//s1 = s2 = "hello world"//Java programming language code
String s1 = "hello";
String s2 = s1;
s1 += " world";
System.out.println(s1 + "\n" + s2);
//s1 = "hello world" and s2 = "hello"In the C++ example, the strings s1 and s2 print the same result because they both point to the same address. In the Java programming language, Strings can't be modified, so the + operator must create a new String when "world" is appended to s1.source: http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.html