1

while comparing to strings we can do using == or .equals()

In == we know that it checks for references but in .equals() it checks for contents.

So suppose if there are 2 strings say

String s="SO"; String s1="SO";

so in this case s1==s and s.equals(s1) both will give true.

But here it gives me false

So what I assume is + is high priority than ==

so in this case

System.out.println(""+s1==s);

it will be splitted like (""+s1)==s and now ""+s1 will be a new String and hence the new String will never be equal to s so its printing false

I am just interested to know whether I thought is right or not

user207421
  • 305,947
  • 44
  • 307
  • 483
rocking
  • 4,729
  • 9
  • 30
  • 45
  • 2
    @SotiriosDelimanolis The link you provided is about final strings,My code does not have final – rocking Feb 26 '14 at 06:24
  • 3
    The answers still stand. Just read them. And read the question carefully, don't look at the title. – Sotirios Delimanolis Feb 26 '14 at 06:25
  • You could fix it by doing `System.out.println((""+s1).intern()==s)` – Edwin Dalorzo Feb 26 '14 at 06:25
  • @rocking - Look at the answers to that question :) – TheLostMind Feb 26 '14 at 06:26
  • @EdwinDalorzo Sorry I am not aware of intern() but I can google what it is.Thanks for suggestion – rocking Feb 26 '14 at 06:28
  • Your assumption about the order of operations can basically be proved to be correct. Otherwise it would print true. – Radiodef Feb 26 '14 at 06:31
  • According to JLS Strings computed by concatenation at run time are newly created and therefore distinct. Regarding the intern method, the java docs say: "Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned." – Edwin Dalorzo Feb 26 '14 at 06:33
  • @Radiodef This is what I wanted to know whether my assumption is right or not. – rocking Feb 26 '14 at 06:34

1 Answers1

1

""+s1 creates a new String Object on the heap (since it is not declared as final). So, the references will not be same.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • Thanks for the answer,As far as I know if We are making `final` then we can not change the content.Does final also means that reference is also not changed – rocking Feb 26 '14 at 06:27
  • 1
    @rocking - Immutable means you cannot change the content.. final (for primitives) means you cannot change the value.. final (for objects) means you cannot reassign the reference to point to some other object. – TheLostMind Feb 26 '14 at 06:29
  • are content and value not the same thing? – rocking Feb 26 '14 at 06:31
  • Well I asked in my question is whether my assumption is right or not.What do you think? – rocking Feb 26 '14 at 06:36
  • now ""+s1 will be a new String and hence the new String will never be equal to s so its printing false. Yes, you are right.. – TheLostMind Feb 26 '14 at 06:38
  • Here ""+s1 means single space will added to the String s1 so it's not equal to the another String s – Prabha Feb 26 '14 at 06:46
  • @Prabha - No. "" is empty string. and its different from " ". (a space). ""+s1 will be created on the heap, So, s1 and ""+s1 will be different. – TheLostMind Feb 26 '14 at 06:48
  • @WhoAmI Empty String will added to s1 right.That's why s1 will created in heap. So concatenation happen hear. thank you – Prabha Feb 26 '14 at 06:52
  • @Prabha - Yes.. you are right. :) – TheLostMind Feb 26 '14 at 06:54