Home  >  Article  >  Java  >  java string comparison method

java string comparison method

一个新手
一个新手Original
2017-10-10 09:39:422544browse

I just recently learned Java, and I encountered a difficult problem today when I was programming, which was about judging whether two strings are equal. In programming, the expression that usually compares whether two strings are the same is "==", but it cannot be written like this in Java. In java, equals() is used;

Example: A string and B and string comparison:

if(A.equals(B)){
}

Return true or false.

String's equals Method used to compare two strings for equality. Since string is an object type, it cannot be judged by simple "==". Use equals to compare whether the contents of two objects are equal.

Note:

equals() compares the contents of the objects (differentiating the case format of letters), but if "==" is used to compare two objects, the two objects are compared. The memory address of the object, so they are not equal. Even if their contents are equal, the memory addresses of different objects are different.

The above is the detailed content of java string comparison method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn