" String ".trim()== "String"为什么是false?

Patrick_DK 2002-01-17 11:51:21
我觉得" String ".trim()虽然返回的是一个新的String对象"String",但是我认为这个新的对象仍然是指向在iteral pool里的"String"啊,不明白为什么会是false?
...全文
202 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
Patrick_DK 2002-01-17
  • 打赏
  • 举报
回复
恩,同意xmvigour(微电)的话
"" String ".trim它相当于是new String("String")"
xmvigour 2002-01-17
  • 打赏
  • 举报
回复
更正:
1、
String s1=" String ";
System.out.println((s1==" String "));
返回true
xmvigour 2002-01-17
  • 打赏
  • 举报
回复
其实大家都还没搞清楚Patrick_DK(疾风摩郎)的意思,他也知道==和equals两者间的区别。
以下是我的测试:
1、
String s1=new String(" String ");
System.out.println((s1==" String "));
返回true
2、
String s1=new String(" String ");
System.out.println((s1==" String "));
返回false

结论:
" String ".trim()虽然返回的是一个新的String对象"String",但它相当于是new String("String")和上面的2是一样的。若new String(" String ") 还是和==" String "指向同一个地址的话,那就没办法实现在程序中有两个一样的字串但属于不同的地址的String对象了。
contributor 2002-01-17
  • 打赏
  • 举报
回复
I have said that "String ".trim() creates a new instance!
It is just like "new String("String");". So it is not in the pool at all!

However, your question is super good!
Patrick_DK 2002-01-17
  • 打赏
  • 举报
回复
to skyyoung(路人甲)

你讲的我是明白的,但是你也肯定清楚,因为iteral pool的缘故
String s1="1111";
String s2="2222";
s1==s2 true;


那么" String ".trim()这个语句是返回一个新的String对象,值为"String".我认为这里没有用new关键字,所以还是指向pool中的那个"String"

那么这么理解的话" String ".trim()=="String"应该是true啊
0legend 2002-01-17
  • 打赏
  • 举报
回复
skyyoung 2002-01-17
  • 打赏
  • 举报
回复
注意两者间的区别。

when you use == with a primitive -int,double , char ... you are checking that the values are identical . but if you use == with an object , you are checking that the 2 objects are stored at the same address. in other words the references pointing to the same object..
Method equals() is different.
it's the same as ==,if it isn't overriden by the object class.
many classes override the method equals().In this case this mehtod will check that content of the object is the same or not,not address.
Hikaru 2002-01-17
  • 打赏
  • 举报
回复
用==比较的是地址,要用equals比较
Patrick_DK 2002-01-17
  • 打赏
  • 举报
回复
to z_yheart(年轻的心)

我觉得这里没有用到new关键字,所以应该还是指向pool里的啊
而且你也说了,trim()在return的时候可能是返回了一个新的String ,那么这个新的"String"(即去掉头尾的空白)和右边的“String”应该都是指向pool里的那个啊。

我们都知道String s1="2222";
String s2="2222";
s1==s2 true
gdsean 2002-01-17
  • 打赏
  • 举报
回复
sorry it supposes to be:" String ".trim().equals("String")==true
z_yheart 2002-01-17
  • 打赏
  • 举报
回复
这个不一定,得看String里面trim()怎么写了,既然不相等,就说明你的推测是错误的,trim()在return的时候可能是返回了一个新的String
contributor 2002-01-17
  • 打赏
  • 举报
回复
"String ".trim() creates a new instance!
The instance of "String ".trim() != the instance of "String".
gdsean 2002-01-17
  • 打赏
  • 举报
回复
" String ".equals("String")==true
Patrick_DK 2002-01-17
  • 打赏
  • 举报
回复
嗯,明白了
xmvigour 2002-01-17
  • 打赏
  • 举报
回复
上面这几话是错的”直接对字符常数的操作都得到同一个对象“
我还以为我在写程序,用//就可以注释了,哈哈……
xmvigour 2002-01-17
  • 打赏
  • 举报
回复
唯一的解释是(我猜的):
对字符常数(immutable)和new String()得到的处理方式是不一样的,
//直接对字符常数的操作都得到同一个对象,所以("String".trim()=="String".trim());

因为:
String s2=" String ";
System.out.println(s2.trim()==" String ".trim());
得到的是false!

哈哈我知道问题在哪了:
System.out.println(" String ".trim()==" String ".trim() );
得到的是false!

If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

当没有空格时 trim()是不会返回一个新的对象的!!!
alula 2002-01-17
  • 打赏
  • 举报
回复
喝喝
TIJ :
You can see that every String method carefully returns a new String object when it’s necessary to change the contents. Also notice that if the contents don’t need changing the method will just return a reference to the original String. This saves storage and overhead.

Java的字符串处理函数有如把 字符串当不可变变量传入函数(类似C++中的const )
。。。
Patrick_DK 2002-01-17
  • 打赏
  • 举报
回复
两边好像都有道理啊
xmvigour 2002-01-17
  • 打赏
  • 举报
回复
但是你试试:
System.out.println((s1.trim()==s1.trim()));你说是什么?
答案是false!
Patrick_DK 2002-01-17
  • 打赏
  • 举报
回复
to 微电

哦,原来以为问题解决了,你说的"" String ".trim它相当于是new String("String")" ,现在我认为还是有问题.

按照你的理解,你来看看
"String".trim()这句话应该是相当于new String("String")了吧
那么再来一遍!再来一个"String".trim(),也相当于又new String("String")了吧
OK,那么这么想的话,因为
new String("String")=new String("String");明显是false吧
那你猜猜"String".trim()=="String".trim()
是true还是false呢?

public class SL275
{
public static void main(String[] args)
{
System.out.println("String".trim()=="String".trim());
System.out.println(new String("String")==new String("String"));
}
}

我试过了,答案是1.true 2.false,你说怎么解释?


加载更多回复(1)

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧