Use the string.equals(Object other) function to compare strings, not the == operator.

The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==, but it's better not to rely on that.

if (usuario.equals(datos[0])) {
    ...
}

NB: the compare is done on 'usuario' because that's guaranteed non-null in your code, although you should still check that you've actually got some tokens in the datos array otherwise you'll get an array-out-of-bounds exception.

Answer from Alnitak on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_equals.asp
Java String equals() Method
The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically. ... If you want to use W3Schools services as an educational institution, ...
Top answer
1 of 16
611

Use the string.equals(Object other) function to compare strings, not the == operator.

The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==, but it's better not to rely on that.

if (usuario.equals(datos[0])) {
    ...
}

NB: the compare is done on 'usuario' because that's guaranteed non-null in your code, although you should still check that you've actually got some tokens in the datos array otherwise you'll get an array-out-of-bounds exception.

2 of 16
593

Meet Jorman

Jorman is a successful businessman and has 2 houses.

But others don't know that.

Is it the same Jorman?

When you ask neighbours from either Madison or Burke streets, this is the only thing they can say:

Using the residence alone, it's tough to confirm that it's the same Jorman. Since they're 2 different addresses, it's just natural to assume that those are 2 different persons.

That's how the operator == behaves. So it will say that datos[0]==usuario is false, because it only compares the addresses.

An Investigator to the Rescue

What if we sent an investigator? We know that it's the same Jorman, but we need to prove it. Our detective will look closely at all physical aspects. With thorough inquiry, the agent will be able to conclude whether it's the same person or not. Let's see it happen in Java terms.

Here's the source code of String's equals() method:

It compares the Strings character by character, in order to come to a conclusion that they are indeed equal.

That's how the String equals method behaves. So datos[0].equals(usuario) will return true, because it performs a logical comparison.

🌐
Reddit
reddit.com › r/learnjava › why to never use == with strings and instead use .equals()
r/learnjava on Reddit: Why to never use == with Strings and instead use .equals()
August 9, 2020 -

In class we were discussing this and basically I understood == tests the EXACT same thing... since Strings are each their own object, we need a special method to check for if they are actually the same words. Furthermore, == tests the address I believe?

Here's the confusing part for me... Java has optimized == so SOMETIMES "hello" == "hello" will return true even if they are two unique Strings... but "hell" += "o" == "hello" returns false. Why ?

🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-and-equals-method-in-java
Difference Between == Operator and equals() Method in Java - GeeksforGeeks
January 4, 2025 - The main difference is that string equals() method compares the content equality of two strings while the == operator compares the reference or memory location of objects in a heap, whether they point to the same location or not.
🌐
Scaler
scaler.com › home › topics › difference between comparing string using == and .equals() method in java
Difference between Comparing String Using == and .equals() Method in Java - Scaler Topics
June 2, 2024 - The equals() method in Java compares two objects for equality. It is defined in the Object class in Java. The equals() method compares characters by characters and matches the same sequence present or not in both objects.
Top answer
1 of 16
762

In general, the answer to your question is "yes", but...

  • .equals(...) will only compare what it is written to compare, no more, no less.
  • If a class does not override the equals method, then it defaults to the equals(Object o) method of the closest parent class that has overridden this method.
  • If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the Object#equals(Object o) method. Per the Object API this is the same as ==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality.
  • Always remember to override hashCode if you override equals so as not to "break the contract". As per the API, the result returned from the hashCode() method for two objects must be the same if their equals methods show that they are equivalent. The converse is not necessarily true.
2 of 16
140

With respect to the String class:

The equals() method compares the "value" inside String instances (on the heap) irrespective if the two object references refer to the same String instance or not. If any two object references of type String refer to the same String instance then great! If the two object references refer to two different String instances .. it doesn't make a difference. Its the "value" (that is: the contents of the character array) inside each String instance that is being compared.

On the other hand, the "==" operator compares the value of two object references to see whether they refer to the same String instance. If the value of both object references "refer to" the same String instance then the result of the boolean expression would be "true"..duh. If, on the other hand, the value of both object references "refer to" different String instances (even though both String instances have identical "values", that is, the contents of the character arrays of each String instance are the same) the result of the boolean expression would be "false".

As with any explanation, let it sink in.

I hope this clears things up a bit.

Find elsewhere
🌐
Javaprogramto
javaprogramto.com › 2020 › 04 › java-compare-two-strings.html
How To Compare Two Strings In Java? (With '==' Operator and equals() Method) JavaProgramTo.com
Let us see what is the difference between "==" and equals() method in Strings Comparision. Is this bad to use "==" operator for strings comparison. When this should be used? Take a look at the following example program that uses "==" operator. package com.java.w3schools.blog.java.program.to.strings; public class StringsCompareDoubleEqualOperator { public static void main(String[] args) { String s1 = "java"; String s2 = "java"; String s3 = new String("java"); String s4 = new String("java"); System.out.println("(s1 == s2) : " + (s1 == s2)); System.out.println("(s1 == s3) : " + (s1 == s3)); Syste
🌐
Java67
java67.com › 2012 › 11 › difference-between-operator-and-equals-method-in.html
Difference between == and equals() method in Java? String Example | Java67
For example java.lang.String class overrides the equals() and hashcode method and in the overridden method, it will check that two string contains same value or character if yes then they are equals otherwise not equal.
🌐
Medium
medium.com › @oguzkaganbati › why-should-we-use-equals-method-instead-operator-for-string-comparison-in-java-510fc50057cc
Why Should We Use “equals()” Method Instead “==” Operator for String Comparison in Java? | by Oğuz Kağan BATI | Medium
March 5, 2024 - By using the equals() method, we’re comparing the content of ‘str1’ and ‘str2’, which in this case is ‘hello’. Therefore, the output is true, indicating that the strings have the same content. In Java, when comparing strings, it’s crucial to use the equals() method instead of the ‘==’ operator.
🌐
Medium
medium.com › @AlexanderObregon › understanding-the-difference-between-equals-and-in-java-10a075326720
Understanding the Difference Between equals() and == in Java
April 24, 2024 - String str1 = new String("Hello"); ... the same object reference, so str1 == str3 returns true. The equals() method in Java is used to compare the content of two objects....
🌐
W3Schools Blog
w3schools.blog › home › string comparison in java
String comparison in java - W3schools
August 27, 2014 - In java there are three ways to compare two strings. 1. By == operator. 2. By equals() method.
🌐
Reddit
reddit.com › r/learnjava › can i use == in java for strings
r/learnjava on Reddit: Can I use == in Java for strings
October 8, 2024 -

My CS teacher told me that you have to use .equals() when comparing strings instead of == but today I tried using == and it worked. Was this supposed to happen or was it like a new update?

public class Main{
public static void main(String[] args) {
    
        String name = "jarod";

        if(name == "jarod"){
            System.out.println("ELLO JAROD");

        }else{
            System.out.print("NO");
        }

    }

}

For this bit of code the output was ello Jarod and if I put in something else for the string variable it gave me no

🌐
DEV Community
dev.to › nikhilxd › understanding-the-difference-between-and-equals-in-java-5d3h
Understanding the Difference Between `==` and `equals()` in Java - DEV Community
May 12, 2024 - The == operator is used to compare two values or object references for equality. When used with primitive data types like int, double, boolean, etc., it compares the actual values stored in those variables.
🌐
TutorialsPoint
tutorialspoint.com › equals-vs-in-java
equals() vs == in Java
There are two ways to determine if two objects are equal in Javal: the.equals() method and the == operator. The.equals() function compares the contents of two objects. The == operator compares the references of two objects. When you create an object
🌐
TestMu AI Community
community.testmuai.com › ask a question
How does Java string compare work, and when should you use `==` vs. `.equals()`? - TestMu AI Community
March 23, 2025 - I’ve been using the == operator to compare strings in my program, but I encountered a bug. After switching to .equals(), the issue was resolved. What is the difference between == and .equals() when comparing strings in …
🌐
Reddit
reddit.com › r/learnjava › help me understand the difference between "==" and ".equals()" in java
r/learnjava on Reddit: Help me understand the difference between "==" and ".equals()" in Java
July 15, 2025 -

I'm currently working on a project that involves comparing strings, but I keep getting stuck on whether to use the "==" operator or the ".equals()" method. From what I've gathered so far, they seem to do the same thing - is it true? Or are there cases where one should be used over the other?

Top answer
1 of 5
49
From what I've gathered so far, they seem to do the same thing - is it true? No. They don't do the same thing. .equals() is a method, so it only applies to objects (reference types). The method compares the content of an object to another. == is an operator - a test for equality on primitive types and for identity (being the same) for objects (reference types). Generally, you should always use .equals with objects - including String values (they also are objects). The exception is if you want to test if two objects (actually their object references) are identical (have the same reference).
2 of 5
38
The difference is a bit hard to explain using strings, so let's use cars instead! Imagine you and your neighbor both own the same car - two different objects but same manufacturer, same color, same number of seats and so on. .equals() will be true because both these cars share the same properties.  However, == will be false because it's two different cars parking in two different parking lots. Let's say you have a daughter and she's learning to drive. For this reason, you allow her to use your car. You set herCar = yourCar. This means herCar == yourCar because both terms/variables ("herCar" and "yourCar") POINT to the exact same car. If you ask someone to point his finger to her car, he'll point at exactly the same car as when you ask him to point at yours. Another example: if someone hits your car with a stone, causing a bruise, this bruise will also exist on your daughter's car because it is the exact same vehicle. However there won't be a bruise on your neighbor's car because yours and his only equal, but they don't ==. Let's go back to strings: .equals() checks whether the strings look the same, have the same length and consist of the same characters in the same order. == checks whether two strings are stored on the same position inside your computer memory. So if you have a string "ExampleString" stored in your memory several times on several positions, == will only be true if you compare variables pointing to the exact same position of the exact string in your computer memory. It will be false otherwise, even if the strings are the same text. This is the case for all objects. For this reason, you should always use equals for objects. --- tl;dr: .equals() means "does it look the same" and can return true even if it's two different objects in your computer memory. == means "is it EXACTLY the same" and can return false even if the objects are 100% equal as long as it's two different objects stored in different positions in your computer memory.
🌐
Quora
quora.com › Is-It-bad-to-use-equals-instead-of-when-comparing-strings-in-Java
Is It bad to use .equals() instead of == when comparing strings in Java? - Quora
Answer (1 of 38): Actually, that’s ... if a.equals(b) is false, then a and b do NOT have the same value. == only compares the memory locations of the strings....