基础问题 String str = "abc" ;和 String str = new String(); str="abc";有什么区别,讲得详细一点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 此回复为自动发出,仅用于显示而已,并无任何其他特殊作用楼主【feptest】截止到2008-07-17 14:24:29的历史汇总数据(不包括此帖):发帖的总数量:1 发帖的总分数:20 每贴平均分数:20 回帖的总数量:8 得分贴总数量:7 回帖的得分率:87% 结贴的总数量:0 结贴的总分数:0 无满意结贴数:0 无满意结贴分:0 未结的帖子数:1 未结的总分数:20 结贴的百分比:0.00 % 结分的百分比:0.00 % 无满意结贴率:---------------------无满意结分率:---------------------如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html 区别就一点,第二个先生成了一个值为空“”的String对象然后做和第一个一样的操作貌似没说,本来也就这么回事 http://topic.csdn.net/u/20080716/11/2dbbb5e6-31a4-4e04-8ff1-0c72167ef24f.html String s1="hello";String s2="hello";如果你去比较 s1==s2 会告诉你是true.这是字符串常量,s1和s2是 匿名String对象 把="hello"赋值给s2的时候不会再创建匿名对象。String s1=new String();s1="hello";String s2=new String();s2="hello";这时你再去比较 s1==s2 系统会告诉你是false因为s1和s2是两个不同的对象 这不还是true吗?你new了一个新对象就放弃引用了 String s1 = new String("hello");String s2 = new String("hello");这样执行s1 == s2会返回false 这不是TURE了,你可以自己编程调试一下,用s1==s2这样去比较的话,是比较地址,s1和s2是两个不同的对象,地址不一样。这个c语言不一样,c语言用这样的等式比较是比较的内容。而java如果用s1.compareTo(s2),返回的结果才是TRUE,这个比较的是内容 ZangXT 说的是对的String s1=new String(); s1="hello"; String s2=new String(); s2="hello"; 还是true String str = "abc" 这里面的abc放在常量池中String str = new String(); str="abc";而这的abc放在堆内存中 怎么都这么晕呢?注意下面两行的区别:String str=new String("abc");String str=new String();str="abc" String s1="hello"; String s2="hello"; 如果你去比较 s1==s2 会告诉你是true. 这是字符串常量,s1和s2是 匿名String对象 把="hello"赋值给s2的时候不会再创建匿名对象。 String s1=new String("hello"); String s2=new String("hello"); 这时你再去比较 s1==s2 系统会告诉你是false 因为s1和s2是两个不同的对象 应该是这样。写错了。ZangXT 是对的。 String s1="hello"; String s2="hello"; 如果你去比较 s1==s2 会告诉你是true. 这是字符串常量,s1和s2是 匿名String对象 把="hello"赋值给s2的时候不会再创建匿名对象。 String s1=new String(); s1="hello"; String s2=new String(); s2="hello"; 这时你再去比较 s1==s2 系统会告诉你是false 因为s1和s2是两个不同的对象 String str="abc"这个abc放在String池里而String str = new String(); str是一个对象 对象引用了堆里的"abc"数据首先存放的位置就不相同使用的时候也不一样的 说的相当详细:http://zangweiren.javaeye.com/blog/209895 String str = "abc" 等价于 String str = new String("abc");-------------------------------------String str = new String(); str="abc"; 等价于String str="";str="abc"; ---------运行结果都是一样的 应该是并不严格等价,考虑常量池问题String str = "abc"是一个“abc”对象在常量池(只有一个String对象)String str = new String("abc");是一个“abc”对象在常量池,一个“abc”对象在heap(有两个String对象)String str = new String(); str="abc";是一个空串“”对象在heap,一个“abc”对象在常量池String str="";str="abc";是一个空串“”对象在常量池,一个“abc”对象在常量池只是使用来讲,并没什么不同。 《算法导论的Java实现》 10 中位数和顺序统计学 java中float型数据是如何存储的? 如何从ArrayList中取值 在用bufferedreader类read方法的时候,当读到流末尾时,会返回-1,如果继续读,是不是继续返回-1呢? 关于File中delete方法的异常问题~~ 运用反射调用,如何传递参数? 2个父画面同时调用1个子,能否在子画面里判断是哪个父画面调起来的? 【500分】求在java和C++中都通用的字符串加密方法? 求Java高手指点 静态变量相关问题 关于StringBuffer比较的相关问题 类和对象生命周期的问题
楼主【feptest】截止到2008-07-17 14:24:29的历史汇总数据(不包括此帖):
发帖的总数量:1 发帖的总分数:20 每贴平均分数:20
回帖的总数量:8 得分贴总数量:7 回帖的得分率:87%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:1 未结的总分数:20
结贴的百分比:0.00 % 结分的百分比:0.00 %
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
然后做和第一个一样的操作貌似没说,本来也就这么回事
String s2="hello";
如果你去比较 s1==s2 会告诉你是true.
这是字符串常量,s1和s2是 匿名String对象 把="hello"赋值给s2的时候不会再创建匿名对象。String s1=new String();
s1="hello";
String s2=new String();
s2="hello";这时你再去比较 s1==s2 系统会告诉你是false
因为s1和s2是两个不同的对象
你new了一个新对象就放弃引用了
String s1 = new String("hello");
String s2 = new String("hello");
这样执行s1 == s2会返回false
String s1=new String();
s1="hello";
String s2=new String();
s2="hello"; 还是true
这里面的abc放在常量池中
String str = new String(); str="abc";
而这的abc放在堆内存中
注意下面两行的区别:
String str=new String("abc");String str=new String();str="abc"
String s2="hello";
如果你去比较 s1==s2 会告诉你是true.
这是字符串常量,s1和s2是 匿名String对象 把="hello"赋值给s2的时候不会再创建匿名对象。
String s1=new String("hello");
String s2=new String("hello");
这时你再去比较 s1==s2 系统会告诉你是false
因为s1和s2是两个不同的对象
应该是这样。
写错了。ZangXT 是对的。
String s2="hello";
如果你去比较 s1==s2 会告诉你是true.
这是字符串常量,s1和s2是 匿名String对象 把="hello"赋值给s2的时候不会再创建匿名对象。 String s1=new String();
s1="hello";
String s2=new String();
s2="hello"; 这时你再去比较 s1==s2 系统会告诉你是false
因为s1和s2是两个不同的对象
而String str = new String();
str是一个对象 对象引用了堆里的"abc"数据
首先存放的位置就不相同
使用的时候也不一样的
http://zangweiren.javaeye.com/blog/209895
等价于
String str = new String("abc");
-------------------------------------
String str = new String(); str="abc";
等价于
String str="";
str="abc"; ---------
运行结果都是一样的
应该是并不严格等价,考虑常量池问题
String str = "abc"是一个“abc”对象在常量池(只有一个String对象)
String str = new String("abc");是一个“abc”对象在常量池,一个“abc”对象在heap(有两个String对象)String str = new String(); str="abc";是一个空串“”对象在heap,一个“abc”对象在常量池
String str="";str="abc";是一个空串“”对象在常量池,一个“abc”对象在常量池只是使用来讲,并没什么不同。