找回密码
 新注册用户
搜索
查看: 2889|回复: 3

奇怪的Java问题

[复制链接]
发表于 2006-1-8 07:33:23 | 显示全部楼层 |阅读模式
I encountered a very strange problem in my java program.

part of the source code:
... ...
String token = st.nextToken();
System.out.println("token="+token);
boolean abc = false;
if (token=="asdf") abc = true;
System.out.println("abc="+abc);

The result displayed in the console is:
token=asdf
abc=false

It's really strange! Since the first result has demonstrated that the value of token is "asdf", how can the statement (token=="asdf") be false?
回复

使用道具 举报

发表于 2006-1-8 08:45:18 | 显示全部楼层
我很少玩java,不过知道在 perl 里,字符串比较不能用“==”,只能用“eq”。不知道java里是不是一样?
回复

使用道具 举报

发表于 2006-1-8 15:45:01 | 显示全部楼层
没接触过JAVA,不知道它对布尔变量的赋值语法是怎样的?
回复

使用道具 举报

 楼主| 发表于 2006-1-9 00:26:47 | 显示全部楼层
Thanks for your help. I've got the answer from another forum.  It will not be true. What this is actually doing is checking the memory location. The two Strings will have different memory locations, therefore making it false.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 新注册用户

本版积分规则

论坛官方淘宝店开业啦~

Archiver|手机版|小黑屋|中国分布式计算总站 ( 沪ICP备05042587号 )

GMT+8, 2024-10-1 13:20

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表