String类基本问题

kaymo 2004-10-18 02:35:12
class test1 {
public static void main( String[] args ) {
String good = "Good";
String s1 = good + "bye";
String s2 = good + "bye";
System.out.println(s1 == s2);
}
}
为什么打印为false

class test2 {
public static void main( String[] args ) {
String s1="hello"
String s2="hello";
System.out.println(s1 == s2);
}
}
为true


...全文
546 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
sylmoon 2004-10-19
  • 打赏
  • 举报
回复
首先要知道equals用来比较两个对象,而==用来比较两个地址是否相等
第一段程序的两个不同的对象地址不同
第二段两个对象的地址都在常量池里,是同一个地址
kaymo 2004-10-19
  • 打赏
  • 举报
回复
up
naxin 2004-10-19
  • 打赏
  • 举报
回复
我只知道,比值,不用 ==

over
AlexTan 2004-10-19
  • 打赏
  • 举报
回复
楼上的精神值得学习!
oceanson23 2004-10-19
  • 打赏
  • 举报
回复
to: kaymo(头发熟了)
我也遇到了你这个问题。详细查书之后得出以下答案
一、首先String和其它基本型别的wrapper classes都是immutable objects(恒常对象),此种对象隶属于read-only classes(唯读类)。这种类的基本意思是它的状况是不可修改的。至于它的详细定义请查看thinking in java的附录。
二、注意+这个运算符。当string 遇到"+"或者"+="时,这两个运算符就会被重载。

这样的话
String good = "Good";
String s1 = good + "bye";
String s2 = good + "bye";
就会等于
String good = "Good";//这个没变
String s1 = good + "bye";
/**这句变为
StringBuffer sb = new StringBuffer(good);
sb.append("bye");//“bye”会自动生成一个bye的String
*/
//也一样
String s2 = good + "bye";
你注意到StringBuffer sb = new StringBuffer(good);
了吧。我想你也明白了。他们都会重新生成一个新的对象。所以它们的句柄是不一样的。

但也把的的代码改了一下。假如是这样的话
class test1 {
public static void main( String[] args ) {
String s1 = "good" + "bye";
String s2 = "good" + "bye";
System.out.println(s1 == s2);
}
}
我原来想遇到+运算符它也应该会同上面一样道理吧,但答案是错的。它输出的是true.
所以我想"good"这些字符串这时候应该不是String的对象吧,不是的话上面的解释就可以说的通了。

具体你最好详细看一下书中的解释,我也只能说出一部分。有什么问题大家再讨论吧。
kaymo 2004-10-19
  • 打赏
  • 举报
回复
to:gibosn(我在听你说)
至于String str1="123",String str2="123"已经很清楚拉
而你说的:
String good = "Good";String s1 = good + "bye";String s2 = good + "bye"; 这种赋值形式时。本身s1和s2的引用是不同的

具体原理是怎样呢?
dreamnear 2004-10-19
  • 打赏
  • 举报
回复
版主呢? 还管不管了啊?java都成了无聊广告版了!!
winterxu416 2004-10-19
  • 打赏
  • 举报
回复
是java自己的优化方法
gibosn 2004-10-19
  • 打赏
  • 举报
回复
这个问题我是这样的来理解的。
在JAVA中。当你这样对一个String类型赋值时:String str1="123",String str2="123",当第一条语句执行时java会在内存中建立一个字符池。当执行第二条语句时,java编译器会首先在学符池中寻找有无相同的字符变量。如果有,会将第一个字符串的地址赋给第二个String类型变量。而使用“==“运算符时,不光对于String类型,对于所有对象的比较都是一样的。是对于对象引用的比较。这时str1和str2是相同的,所以相同。
而当你采用:
String good = "Good";String s1 = good + "bye";String s2 = good + "bye"; 这种赋值形式时。本身s1和s2的引用是不同的。所以结果为false。你可以采用equals()方法来进行判断两个字符串是否相等。如果要对字符串排序,可以用compareTo().
cykstar 2004-10-19
  • 打赏
  • 举报
回复
可以利用反射来查看一下就明白了!
zhousqy 2004-10-19
  • 打赏
  • 举报
回复
看PRACTICAL JAVA
goingmm 2004-10-19
  • 打赏
  • 举报
回复
.
gyf168 2004-10-19
  • 打赏
  • 举报
回复
class test1 {
public static void main( String[] args ) {
String good = "Good";
String s1 = good + "bye";
String s2 = good + "bye";%%%%%创建了一个新的String对象
%%%%%%%%% 如果改为String s2 == s1;结果:ture%%%%
System.out.println(s1 == s2);
附加一条%%%%%%%%%%%%%
System.out.println(s1.equals(s2));
则屏幕显示为:%%%%%%%%%5
ture %%%%%%%%%%
ture %%%%%%%%%%
}
}
为什么打印为false

class test2 {
public static void main( String[] args ) {
String s1="hello"
String s2="hello";
System.out.println(s1 == s2);
}
}
为true
test2的程序可以写为下面的
class test1 {
public static void main( String[] args ) {
String hello, s1, s2;
hello = "hello";
s1 = hello;
s2 = hello;
System.out.println(s1 == s2);
}
}
test2实际上是声明了3个String变量hello,s1,s2
将字符变量"hello"赋值给hello,再将hello赋值给s1,再将s1赋值给s2;
==在用于对象的时候不是检查2个对象的值是否相等
而是判断运算符两边引用的是否是同一个对象
test2的hello,s1,s2引用的就是同一个对象
而test1是给3个变量赋了相同的值



chengyy02 2004-10-18
  • 打赏
  • 举报
回复
class test1 {
public static void main( String[] args ) {
String good = "Good";
String s1 = good+"" ;
String s2 = good+"";
System.out.println(s1 == s2);
}
}
运行为false
而去了""又为true
dingtang 2004-10-18
  • 打赏
  • 举报
回复
有点乱
插入的部分用%%%开头
dingtang 2004-10-18
  • 打赏
  • 举报
回复
class test1 {
public static void main( String[] args ) {
String good = "Good";
String s1 = good + "bye";
String s2 = good + "bye";%%%%%创建了一个新的String对象
%%%%%%%%% 如果改为String s2 == s1;结果:ture%%%%
System.out.println(s1 == s2);
附加一条%%%%%%%%%%%%%
System.out.println(s1.equals(s2));
则屏幕显示为:%%%%%%%%%5
ture %%%%%%%%%%
ture %%%%%%%%%%
}
}
为什么打印为false

class test2 {
public static void main( String[] args ) {
String s1="hello"
String s2="hello";
System.out.println(s1 == s2);
}
}
为true
test2的程序可以写为下面的
class test1 {
public static void main( String[] args ) {
String hello, s1, s2;
hello = "hello";
s1 = hello;
s2 = hello;
System.out.println(s1 == s2);
}
}
test2实际上是声明了3个String变量hello,s1,s2
将字符变量"hello"赋值给hello,再将hello赋值给s1,再将s1赋值给s2;
==在用于对象的时候不是检查2个对象的值是否相等
而是判断运算符两边引用的是否是同一个对象
test2的hello,s1,s2引用的就是同一个对象
而test1是给3个变量赋了相同的值
===========================================================================
小弟初来乍到
还望各位大哥指点
叉子 2004-10-18
  • 打赏
  • 举报
回复
JAVA的string constants(常量)是共享的,当运用subString()方法时,将另外创建一个string。
q_h_q 2004-10-18
  • 打赏
  • 举报
回复
我觉得楼主这个问题很好,说明很细心。

我觉得情况是这样的
String good = "Good";
String s1 = good + "bye";
String s2 = good + "bye";
这样一来变量s1和s2分别指向两个不同的变量(因为用变量good和"+"号返回两个不同的变量了,故用==不会相等)
而如果你写成这样的形式:
String s1 = "good"+ "bye";
String s2 = "good"+ "bye";

那么用==号比较肯定返回true

kaymo 2004-10-18
  • 打赏
  • 举报
回复
呵呵 其实 lin_li_00(lin_li_000)应该是对的吧
但讲得不是很清楚......
模糊ing
zez 2004-10-18
  • 打赏
  • 举报
回复
呵呵,这种问题很多经典的帖子,自己去找..
呵呵,讨论这种问题的,一批讨论完了又来一批 :)
这是java最基本的了...
加载更多回复(20)

62,614

社区成员

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

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