Como comparar dos variables de tipo String en java?. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Como comparar dos variables de tipo String en java?.

Si comparo dos String con el mismo texto mediante == porque no me da true?

28th Sep 2017, 1:21 PM
Luis Gerardo Collazos Castro
Luis Gerardo Collazos Castro - avatar
1 Answer
+ 13
The "==" operator compares the value of two object references to see whether they refer to the same String instance while the equals() method compares the "value" inside String instances (on the heap) irrespective of the two object references refer to the same String instance or not. P.S. That's the reason why "==" resulted in false! You should use the equals() method when comparing two Strings.
28th Sep 2017, 1:47 PM
Dev
Dev - avatar