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,290 points
19 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,020 points
25 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,060 points

edited Mar 15 by Kalgi 30 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
350 views
0 votes
1 answer

What is the difference between jdk and jre?

JRE: It stands for Java Runtime Environment. ...READ MORE

answered Apr 20, 2018 in Java by Akrati
• 3,150 points
70 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,360 points

edited Nov 6, 2018 by Omkar 4,489 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,030 points
1,209 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,030 points
3,474 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,030 points
214 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
38 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
16 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.