Topic: String s = new String("Cleanser");t和String s="Cleanser"有什么区别

  Print this page

1.String s = new String("Cleanser");t和String s="Cleanser"有什么区别 Copy to clipboard
Posted by: alexzlw
Posted on: 2006-04-24 08:56

String s = new String("Cleanser");
和String s = "Cleanser" 有什么区别

2.Re:String s = new String("Cleanser");t和String s="Cleanser"有什么区别 [Re: alexzlw] Copy to clipboard
Posted by: Skybus
Posted on: 2006-04-24 10:04

http://www.cjsdn.net/post/view?bid=1&id=80131&sty=3&age=0&tpg=1&ppg=1#80131

3.Re:String s = new String("Cleanser");t和String s="Cleanser"有什么区别 [Re: alexzlw] Copy to clipboard
Posted by: hailang1986
Posted on: 2006-04-24 13:53

效果一样。
多看看JAVA书就有啦

4.Re:String s = new String("Cleanser");t和String s="Cleanser"有什么区别 [Re: hailang1986] Copy to clipboard
Posted by: zcjl
Posted on: 2006-04-24 15:29

hailang1986 wrote:
效果一样。
多看看JAVA书就有啦

效果不一样,建议你自己多看看书先

5.Re:String s = new String("Cleanser");t和String s="Cleanser"有什么区别 [Re: alexzlw] Copy to clipboard
Posted by: hailang1986
Posted on: 2006-04-24 19:52

楼上的不一样吗?我是新人,不吝赐教

书上是这样解说的(JAVA2程序设计实用教程,陈帆编,机械工业出版社)

字符串为参数创建新对象-----String(字符串参数)
其中,参数有三种形式:字符串常量,String对象或StingBuffer类对象,如:
String s=new String("Hello Java!");
还可以先定义一个Sting类对象,然后为其赋值,如:
String s;
s="Hello Java!";
一种更简洁的方法创建字符串常量,如:
String s="Hello Java!";

6.Re:String s = new String("Cleanser");t和String s="Cleanser"有什么区别 [Re: alexzlw] Copy to clipboard
Posted by: java8964
Posted on: 2006-04-25 05:51

都问了N遍。

为什末不search before you ask?

这篇文章解释的很不错:
http://www.cjsdn.net/post/view?bid=1&id=172617&sty=1&tpg=2&age=0

7.Re:String s = new String("Cleanser");t和String s="Cleanser"有什么区别 [Re: hailang1986] Copy to clipboard
Posted by: zcjl
Posted on: 2006-04-25 09:22

hailang1986 wrote:
楼上的不一样吗?我是新人,不吝赐教

主要是对象放置在堆还是栈的区别。
String str1 = new String("this is str1");每次都从堆中申请空间,并创建新的String对象
而String str2 = "this is str2";则是先检查栈的String池中是否存在"this is str2"这个对象。如果有,直接将引用str2指向这个对象;如果没有,则需要创建新对象"this is str2"并放入String池中,然后将引用str2指向这个对象。
下面的代码结果是false false true true
String str1 = new String("this is string");
String str2 = new String("this is string");
String str3 = "this is string";
String str4 = "this is string";
System.out.println((str1 == str2) + " " + (str1 == str3) + " " + (str3 == str4) + " " + (str1.intern() == str3));


前面有个回复链接的那个帖子讲得更详细些

8.Re:String s = new String("Cleanser");t和String s="Cleanser"有什么区别 [Re: zcjl] Copy to clipboard
Posted by: hailang1986
Posted on: 2006-04-25 09:37

zcjl wrote:
主要是对象放置在堆还是栈的区别。
String str1 = new String("this is str1");每次都从堆中申请空间,并创建新的String对象
而String str2 = "this is str2";则是先检查栈的String池中是否存在"this is str2"这个对象。如果有,直接将引用str2指向这个对象;如果没有,则需要创建新对象"this is str2"并放入String池中,然后将引用str2指向这个对象。
下面的代码结果是false false true true
String str1 = new String("this is string");
String str2 = new String("this is string");
String str3 = "this is string";
String str4 = "this is string";
System.out.println((str1 == str2) + " " + (str1 == str3) + " " + (str3 == str4) + " " + (str1.intern() == str3));


前面有个回复链接的那个帖子讲得更详细些

楼上的,强啊。新手不懂,谢谢赐教,我把上面那个回复的贴子收藏了,
就此沉贴了,不然就造成灌水嫌疑了

9.Re:String s = new String("Cleanser");t和String s="Cleanser"有什么区别 [Re: alexzlw] Copy to clipboard
Posted by: yangqw010
Posted on: 2006-04-29 14:52

呵呵 不错


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923