What is difference between .equals() and .matches() when comparing strings. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What is difference between .equals() and .matches() when comparing strings.

any minor difference or no difference

28th Mar 2017, 2:46 AM
arpit dixit
arpit dixit - avatar
7 Answers
+ 16
The method equals checks if two strings are identical. The method matches checks, if the string matches the regular expression you are passing. If you have questions about a particular class, it is the best way to have a look at the javadoc, for String see https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
28th Mar 2017, 4:44 AM
Tashi N
Tashi N - avatar
+ 14
@Tashi what is your age
28th Mar 2017, 4:48 PM
Ekansh
+ 14
@Ekansh I am stone-age. Some of my best friends were Neanderthal people.
28th Mar 2017, 4:57 PM
Tashi N
Tashi N - avatar
+ 13
@salman Your definition for equals isn't correct. Look it up at the javadocs. [Edit] Corrected his answer now 👍
28th Mar 2017, 4:45 PM
Tashi N
Tashi N - avatar
+ 6
do u work for solo learn
1st Apr 2017, 11:26 AM
arpit dixit
arpit dixit - avatar
+ 1
.equals() - check the two strings are equal. .matches() - checks the regular expression or condition matched or not
28th Mar 2017, 9:26 PM
Mohammed Salman Khan
Mohammed Salman Khan - avatar