Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Whether java string comparison should use = = or equals

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "java string comparison should be used = = or equals", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "java string comparison should be used = = or equals" it!

When a string is defined with new String ("aaaa"), = = returns false,equals and returns normal results.

such as

System.out.println ("a" = = "a"); / / true System.out.println ("a" .equals ("a")); / / true System.out.println ("a" .equals (new String ("a"); / / true System.out.println ("a" = = new String ("a")); / / false System.out.println (new String ("a") = = new String ("a") / / false System.out.println (new String ("a") .equals (new String ("a")); / / true

The above are mentioned in almost all the articles on the Internet, and they are only mentioned. However, in the actual project, there is no reference value.

I've never seen anyone define strings in the new String way, but when they use = = to make a judgment, they can still return the wrong result.

The reason is that if you don't use new String, someone will use it for you.

In general development, strings are always used along with the methods of the String class, where new String is heavily used. Once these methods are used, = = produces a variety of bug.

System.out.println ("a" .concat ("a") = = "aa"); / / false System.out.println ("aa" .replaceAll ("a", "b") = = "bb"); / / false System.out.println ("A" .toLowerCase () = "a"); / / false System.out.println ("1X 2" .split (";") [0] = = "1"); / / false

What's more, these methods can also return true under certain circumstances, and the specific reason can be found by reading the source code.

System.out.println ("a" .concat (") =" a "); / / ture / / replaceAll will always return false System.out.println (" a ".toLowerCase () =" a ") due to the use of StringBuffer's toString method; / / ture System.out.println (" 1x "2" .split ("3") [0] = "1x x 2"); / / ture

This can lead to some problems:

There was no bug during the test, but suddenly a bunch of bug appeared after being online.

The module has been in a state of sometimes right and sometimes wrong.

When doing the extension, some methods of the String class were added in the upper layer, which led to problems in the lower layer.

... Wait

Either way, it's a very depressing bug.

You think it's easy? When you get out of bug, you won't think so!

When using equals, it is important to note that the caller cannot be null, otherwise a null pointer exception will be reported. It can generally be avoided by calling equals with a constant. If there are unknowns on both sides, we can only judge whether it is empty or not. It's a bit of trouble, but it's better than bug.

String str1 = "a"; String str2 = "a"; System.out.println ("a" .equals (str1)); / / correct, no null pointer System.out.println (str1.equals ("a")); / / not recommended. If str1 is empty, null pointer System.out.println (str1! = null) & & str1.equals (str2)) | | (str1 = = null & & str2 = null)); / / correct, but it is recommended to break it into two sentences. Don't you find it a headache to have so many logical judgments?

Conclusion

Just pretend that = = does not exist, no brain uses equals.

Thank you for reading, the above is the content of "java string comparison should use = = or equals". After the study of this article, I believe you have a deeper understanding of whether java string comparison should use = = or equals, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report