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
41 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,640 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,050 points
72 views
0 votes
2 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 in Java by Sirajul
• 40,990 points
571 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
130 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 in Java by Piya
75 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 11,295 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,110 points
2,417 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,110 points
6,255 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,110 points
779 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 in Java by Sirajul
• 40,990 points
175 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,640 points
31 views