-6

I'm only new to java and trying to make an equals code, but it won't work with ==, only with .equals() not sure why.

    import java.lang.*;
    import java.util.*;

    public class password
    {
        public static void main(String args[]) 
        {
            Scanner Keyboard = new Scanner(System.in);
            String guess = Keyboard.newLine();

            String password = "1password";

            if (guess == password) {
                System.out.println("Welcome");
            } else {
                System.out.println("Login Failed");
            }

        }
    }
dbut
  • 1
  • 1
  • 2
    When you are new to a language which has been around for more than 15 years, you can assume a) most questions you have have been answered before and a quick search will avoid annoying people who see these question come up again and again., b) if it doesn't work the way you think it is not a bug, its a "feature" now. ;) – Peter Lawrey Aug 06 '13 at 08:10

4 Answers4

1

I'm only new to java and trying to make an equals code, but it won't work with ==, only with .equals() not sure why

because == compares object references NOT the contents of the string. You can find a great explanation at theJavaGeek

  • == checks whether two variables refer to the same object.
  • equals() method checks whether the contents of the object are same or not.
  • so If == returns true, then equals() method also returns true because they are referring to the same object hence they are equal(By equals() contract one object should be equal to itself)
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • `so If == returns true, then equals() method also returns true` yes, that's true for `String` and for any other class a reasonable being wrote. But that's not always the case. – Pawel P. Aug 06 '13 at 14:48
  • @JohnN. I was trying to explain the equals contract. An object should be equal to itself. When two variables refer to the same object, then `==` and `equals()` should return true – Prasad Kharkar Aug 06 '13 at 14:52
  • I agree. An object SHOULD be equal to itself. But it is not guaranteed (by the language or JVM or whatever) that it always is. Many "funny" bugs was found with very poorly written classes that didn't obey the most fundamental of all `equals` contract requirements. – Pawel P. Aug 06 '13 at 14:55
  • 1
    Not guaranteed ... we should do it :). I edited my answer according to your suggestions :) thanks for that – Prasad Kharkar Aug 06 '13 at 14:57
  • @JohnN. always welcome :) After all we are all students – Prasad Kharkar Aug 06 '13 at 15:04
0

Use the String.equals(String otherString) function to compare strings, not the == operator.

The reason is that == just compares object references,where as .equals() checks equality.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Strings should only be compared with .equals(), because with == you compare the Objects which are different.

Loki
  • 4,065
  • 4
  • 29
  • 51
0

trying to do == with string is checking reference equals. if the strings are exactly the same, meaning referenced to the same place, then it will be true, otherwise false

doing equals() check if the string matches, so if the strings contains the same values then you'll get true

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70