0

For the below code:

String bis = "good";
String cis  ="good";
char[] ch = {'g','o','o','d'};
String temp="";
for (char c:ch){
    temp=temp+c;
        }
System.out.println(temp);

System.out.println(cis==bis); //true
System.out.println(bis==temp); //false

'temp' variable evaluates to "good" same as 'bis' and 'cis' but bis==temp returns false unlike bis==cis which evaluates to true.Please let me know why this happens? In String pool "good" object is there.So 'temp' reference should ideally be pointing to it right as done by bis and cis?

  • You should use .equals method to compare string's, not ==. – Krishnanshu Gupta May 14 '17 at 17:40
  • 2
    @KrishnanshuGupta the question is not "how to compare strings". – Cargeh May 14 '17 at 17:42
  • Not if we want to compare referential equality. – Teja duggirala May 14 '17 at 17:42
  • But if you use .equals() the value returned is true. Try it for yourself. – Krishnanshu Gupta May 14 '17 at 17:43
  • 1
    @KrishnanshuGupta again, that's not what he's asking. If you want to see if two objects given to you are actually the same object (referential equality), you don't use .equals(). – Cargeh May 14 '17 at 17:45
  • 1
    @Cargeh, I'm not answering the question, because a minute long google search can help the user more than I can. I am just telling the user how to make their program work. – Krishnanshu Gupta May 14 '17 at 17:45
  • 1
    @KrishnanshuGupta Yes bro It returns true.Please tell me why bis==cis returns true and temp==bis returns false.As all of them are referring to "good" object. – Teja duggirala May 14 '17 at 17:46
  • "*In String pool "good" object is there.So 'temp' reference should ideally be pointing to it*" what makes you think so? – Pshemo May 14 '17 at 17:47
  • @Pshemo From what I know if we create new String using String a ="good"; First it will be checked in String pool to see if there is any String with same value then it will be assigned or else JVM creates a new String Object and puts it in String pool. And also bis==cis tells us that they are refering to same object because once bis creates "good" in pool then when cis is getting created it sees "good" already being there in pool so the same objet will be allotted to cis.Hence bis==cis returns true. – Teja duggirala May 14 '17 at 17:50
  • 1
    Java is using string pool for literals only, since there is bigger chance that they may need to be reused. But it is different case for strings which ware created dynamically. Should Java put and reuse all strings which ware created during `for (char c:ch)` in pool? `"g"` and `"go"` and `"goo"` and finally `"good"`? – Pshemo May 14 '17 at 17:50
  • @Tejaduggirala The == checks the references of both the String's, but the .equals() method checks the value/contents of them. – Krishnanshu Gupta May 14 '17 at 17:51
  • 1
    @Tejaduggirala unfortunately, I can't give you the detailed answer (the question is closed). Basically, if you look at byte-code that your method produces, you'll see that the character concatination in the for-each loop is actually done through StringBuilder (hidden optimization, so to say). And in the end it calls `StringBuilder.toString()`, which in turn returns `return new String(value, 0, count);` So it's not from the string pool – Cargeh May 14 '17 at 17:54
  • @KrishnanshuGupta bro you are not getting me. – Teja duggirala May 14 '17 at 17:55
  • @KrishnanshuGupta OP is not asking how to correct this behavior, but what is causing it. In other words, why `temp` doesn't hold string from string pool like `bis` and `cis`. – Pshemo May 14 '17 at 17:56
  • 1
    I wish you could re-open the question, cos it's rather interesting and marked as duplicate wrongly. I'd provide a detailed answer with byte-code – Cargeh May 14 '17 at 17:57
  • 1
    String pooling only works "out of the box" for string constants. If you begin concatenating `String`s to an "new" `String` that has a value of an already known constant, pooling will not work. This is why we have [`String.intern()`](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#intern--). – Turing85 May 14 '17 at 17:58
  • @Cargeh I agree that duplicate is not valid, but I still believe it was already answered elsewhere. I am searching for better duplicate target so I could change it. – Pshemo May 14 '17 at 17:58
  • @Cargeh Yes I am trying to reopen it.Thank you,you understood me clearly. – Teja duggirala May 14 '17 at 18:00
  • @Pshemo Thank you,you got me.I will try to reopen the question. – Teja duggirala May 14 '17 at 18:01
  • 2
    It's a closer duplicate of [this question](http://stackoverflow.com/q/30275117/3788176)... But guess what that's a duplicate of. – Andy Turner May 14 '17 at 18:04
  • @AndyTurner yes, but the first answer is spot-on. – Turing85 May 14 '17 at 18:05
  • Thanks @AndyTurner I think I get it.I can look into this to know more. – Teja duggirala May 14 '17 at 18:08
  • 1
    In short `temp=temp+c;` is similar to `temp = new StringBuilder(temp).append(c).toString()`. So each time you are creating new StringBuilder object which will hold previous value of `temp`, then add new value and convert it to String. StringBuilder is not related to string pool since it doesn't really make much sense. – Pshemo May 14 '17 at 18:14

0 Answers0