永远不要使用==来比较字符串
来源:互联网 发布:淘宝商品主图片尺寸 编辑:程序博客网 时间:2024/05/01 07:16
最近在看《Core Java 2》,有种相见恨晚的感觉,比先前看得几本书都要好很多,其中很多讲解堪称经典。其中关于测试字符串是否相等的部分就非常清楚。原文如下:
Do not use the == operator to test whether two strings are equal! It only determines whether or not the strings are stored in the same location. Sure, if strings are in the same location, they must be equal. But it is entirely possible to store multiple copies of identical strings in different places.
Do not use the == operator to test whether two strings are equal! It only determines whether or not the strings are stored in the same location. Sure, if strings are in the same location, they must be equal. But it is entirely possible to store multiple copies of identical strings in different places.
String greeting = "Hello"; //initialize greeting to a stringIf the virtual machine would always arrange for equal strings to be shared, then you could use the ==constants are shared, not strings that are the result of operations like + or substring. Therefore, never use == to compare strings lest you end up with a program with the worst kind of bug—an intermittent one that seems to occur randomly. operator for testing equality. But only string
if (greeting == "Hello") . . .
// probably true
if (greeting.substring(0, 3) == "Hel") . . .
// probably false
- 永远不要使用==来比较字符串
- 永远不要使用 Boolean 对象
- ,永远不要去比较float和double
- Java中使用==和使用equals来比较两个字符串的区别
- 永远不要使用C++的“重定义”
- Integer之间比较相等最好不要使用==
- 像你这样的滚蛋!永远不要来香格里拉!
- 永远不要比较两个浮点数是否相等
- PHP Float 永远不要比较两个浮点数是否相等
- 不要使用连接来合并多个字符串,应该使用StringBuilder来提高性能
- 【Java编码准则】の #11不要使用Object.equals()来比较密钥值
- 为什么你应该永远不要再使用MongoDB
- 永远不要停止进步
- 永远不要忘记学习
- 永远不要解释你自己
- 永远不要相信自己
- 永远不要用QQ
- 永远不要等待
- asp.net连接数据库
- 年终总结书写要点
- 数据操作1——command_nonquery
- asp.net数据库操作2——nonquery
- asp.net数据库操作3——reader
- 永远不要使用==来比较字符串
- asp.net数据库操作4——scalar
- modifier
- Command设计模式应用时的几个问题
- 不轻易打断别人的工作--给刚参加工作的同学们的一个建议
- applet中添加音乐
- 翻译辨误-翻习语不要想当然
- Applet中访问服务器端的图片
- 美国生活常用习语