0

Can someone explain to me why the following code as shown doesn't display "IS equal", but if I change the assignment to ip to the commented one it does?

public class cl {

    public static void main(String[] args) {
        String temp = null;
        String a = null;
        String b = null;
        String ip = null;

        ip = "DateTime  ";
//      ip = "DateTime";
        b =  "DateTime";

        a = ip.substring(0, 8);

        if (a == b) {
            System.out.println("IS equal");
        }
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • use `if(a.equals(b))` instead of `a==b` – Ridwan Jul 09 '20 at 18:09
  • It works - you are a gentleman and a scholar - thanks ... but why? – Jeff Prisco Jul 09 '20 at 18:20
  • the `==` operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with `==`, but it's better not to rely on that. – Ridwan Jul 09 '20 at 18:25

0 Answers0