-1

I am encountering a weird problem. I was trying to compare two 128 bits strings and I believe every char of them match each other(and I tested comparing String.chartAt(#) several times) but when I do if (String 1== String 2) .. else .. It went to else clause. Why is that?

Ava
  • 9
  • 1
  • 1
    use String1.equals(String 2) – Foolish Apr 21 '17 at 03:16
  • Weird things: the number of newbies that don't do the 5 seconds of research required to resolve this issue... But prefer to spend 5 minutes to write up a question here. Then: that strange correlation between : speed of incoming answers vs question being an obvious dup. – GhostCat Apr 21 '17 at 03:24

1 Answers1

0

When using == for comparison, you are checking if both variables refer to the same object (reference comparision). You should use strings equals() method in order to compare if they are equal in a sense they consist of same characters.

description of equals method in java documentation

public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Kamil Piwowarski
  • 506
  • 5
  • 15