Difference between String.equals() and ==

0 votes

I have the below code, in which I have a string which I am breaking down into tokens and storing them into an array of String afterward. Then I am trying to compare the first array with a variable using == but it's not giving me any output but when I am using String.equals() it is giving me the result. So, can someone explain me the difference between String.equals and ==?

public static void main(String...aArguments) throws IOException {

    String userId = "Andrew";
    String password = "236475";

    String userData = "Andrew 236475";
    StringTokenizer tokens = new StringTokenizer(userData, " ");
    int n_uData = tokens.countTokens();
    String[] u_Data = new String[n_uData];
    int i = 0;

    while (tokens.hasMoreTokens()) {
        String str = tokens.nextToken();
        u_Data[i] = str;
        i++;
    }

    //System.out.println (userId);

    if ((u_Data[0] == userId)) {
        System.out.println("WORKING");
    }
}

May 7, 2018 in Java by v.liyyah
• 1,300 points
24 views

1 answer to this question.

0 votes

Though both are used for comparison, but the way they both works are pretty much different.

== is called the comparison operator. This compares the references of the operands, that is the physical address in which the variables are stored. It returns true if and only if both variables refer to the same object.

String.equals() is a pre-built function of String class which compares the values stored in the objects. It checks nothing except for the value specified in the function.

Thus in your code when you are using == it is comparing the reference of userId and u_id whereas when using equals() it is comparing the values stored in them.

image

answered May 7, 2018 by geek.erkami
• 2,320 points

Related Questions In Java

0 votes
2 answers

What is the difference between = and equals()?

The equals() method compares the "value" inside String instances ...READ MORE

answered Aug 13, 2018 in Java by Daisy
• 8,040 points
34 views
0 votes
1 answer

What is the difference between string object and string literal?

Difference between string objects and string literals A ...READ MORE

answered Feb 28 in Java by Avantika
• 1,500 points

edited Mar 15 by Kalgi 50 views
+1 vote
1 answer

Can you please explain the difference between string, string buffer and string builder?

String are immutable where as StringBuffer and ...READ MORE

answered Jul 4 in Java by Akash
• 160 points
49 views
0 votes
2 answers

What is the difference between implements and extends?

Extends : This is used to get attributes ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,190 points
408 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 6,141 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,050 points
1,618 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,050 points
4,312 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,050 points
333 views
0 votes
1 answer

Fetch list of in-between dates using Java

I tried generating the list of the ...READ MORE

answered May 10, 2018 in Java by geek.erkami
• 2,320 points
52 views
0 votes
1 answer

Replacing a String with a character

Yes, you are missing out on a ...READ MORE

answered May 16, 2018 in Java by geek.erkami
• 2,320 points
19 views