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
894 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,680 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,140 points
1,398 views
+1 vote
4 answers

What is the difference between string object and string literal?

A String literal is a Java language concept. This ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,230 points
67,433 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 5, 2019 in Java by Akash
• 160 points
1,340 views
0 votes
1 answer

What is the difference between main(String[] args) and main(String... args) in Java?

String[] is used to accept a single parameter ...READ MORE

answered Sep 6, 2019 in Java by Piya
3,737 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 81,222 views
+1 vote
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,500 points
8,358 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,500 points
17,432 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,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,875 views
0 votes
2 answers

Fetch list of in-between dates using Java

java.time Package The new java.time.package in Java 8 incorporates ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
5,823 views
0 votes
1 answer

Replacing a String with a character

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

answered May 17, 2018 in Java by geek.erkami
• 2,680 points
627 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP