0

So I was taught that “==” only checks whether they are the same object and “equals()” checks the values of the objects.

But for Strings, I found that “==” also checks for values. For example:-

    String x = "apple";
    String y = "orange";
    x=y;

    if (x==y){
        System.out.println("yes");
    }else{
        System.out.println("no");
    }

//OUTPUT: yes

or

String x = "apple";
String y = "apple";
y = "watermelon";
    if (x==y){
        System.out.println("yes");
    }else{
        System.out.println("no");
    }tem.out.println("no");
    //OUTPUT: no
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Pavitran
  • 11
  • 1
  • 1
    This is the case due to optimization/implementation specifics including a constant pool and interning, but cannot be guaranteed. Unless you are doing something extremely specific to the internals of a JVM, you should probably use `equals()` for string objects. – nanofarad Apr 04 '17 at 17:11
  • What you are seeing is correct but what you understand is wrong. String has a concept called String Constant Pool. – Alekhya Apr 04 '17 at 17:13
  • 2
    *"But for Strings, I found that “==” also checks for values."* No, it doesn't. In your first example, `x` and `y` both refer to the same object because you've expressly assigned `y` to `x` before doing the comparison, and so `x == y` is true. In your second example, `x` and `y` refer to the same object because of the way strings created via literals are automatically [interned](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#intern--). But given `x = new String("apple"); y = "apple";`, `x == y` would be false; they refer to different objects. Details in the linked q's answers. – T.J. Crowder Apr 04 '17 at 17:13
  • 1
    Re your (now rolled-back) edit: *"I dont know why is my question tagged duplicate, the other question thats is supposedly similar to mine does not answer my question"* Yes, it does; or more accurately, its answers do. Read them more carefully, in conjunction with the comments above. Also, meta-content like that doesn't go in questions. If you want to comment on the question, use the "add comment" link. – T.J. Crowder Apr 04 '17 at 17:18
  • 1
    Note that equals() doesn't necessarily check the contents of an object: that's down to the implementation. The default implementation of equals() actually does call ==. – Bathsheba Apr 04 '17 at 17:20
  • alright sorry man, im new to this, take it easy on me bro – Pavitran Apr 04 '17 at 17:21
  • Not trying to be hard on you. :-) Just trying to explain how the site works, and help within the bounds of how the site works. Sorry, sometimes terse comes across wrong. – T.J. Crowder Apr 04 '17 at 17:22
  • I don't think anybody is trying to upset you. You ask a good question IMHO. Personally I don't think the duplicate is perfect since it doesn't cover the difference between == and equals(). I can't resist saying that all the string kludges in Java are a source of many questions. – Bathsheba Apr 04 '17 at 17:22
  • @Bathsheba: Yes, I thought so too (about the question). – T.J. Crowder Apr 04 '17 at 17:24

0 Answers0