-5

If string literals are being added on the string constant pool and the each of the String methods are creating a new String Object does it mean that the String objects returned by these methods are being added on the constant pool?

"String".replace('g','G') == "String".replace('g','G')

the code above prints false even though the outcome of the replace is "StrinG" since the replace method returns a new String Object then it just happens that both sides of the "==" are of different String Object that happens to have the same character sequence which is "StrinG"?

**edit:**as commented by Pshemo, yes I do know the difference between "==" and "equals()", the question is that if by any chance does the String outputed by the methods are being added also on the constant pool.

anathema
  • 947
  • 2
  • 15
  • 27
  • "does it mean that the String objects returned by these methods are being added on the constant pool?" why would they? They are not literals, they are new strings which simply ware created based on literal. – Pshemo Jun 23 '15 at 14:45
  • duplicate of http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – MadConan Jun 23 '15 at 14:46
  • @MadConan was going to link to that post too ^^ – Arthur Eirich Jun 23 '15 at 14:46
  • 1
    @MadConan OP seems to know difference between `==` and `euqals`. Question is if `replace` place result string in String Pool or not. – Pshemo Jun 23 '15 at 14:48
  • Some elementary Java tips: Strings in java are immutable; == for reference objects in Java compares that they point to the same object, not that they have the same value; string objects created at run-time are not interned in the constant pool or anywhere else (although there is nothing in the spec to forbid this) – antlersoft Jun 23 '15 at 14:49
  • @Pshemo It seems like it, but the behavior, documentation and debugging would easily answer the question so I don't think the OP really does know the difference. – MadConan Jun 23 '15 at 14:49

1 Answers1

5

From the replace docs:

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

Original source code:

public String replace(char oldChar, char newChar) {
    if (oldChar != newChar) {
        int len = count;
        int i = -1;
        char[] val = value; /* avoid getfield opcode */
        int off = offset;   /* avoid getfield opcode */

        while (++i < len) {
            if (val[off + i] == oldChar) {
                break;
            }
        }
        if (i < len) {
            char buf[] = new char[len];
            for (int j = 0 ; j < i ; j++) {
                buf[j] = val[off+j];
            }
            while (i < len) {
                char c = val[off + i];
                buf[i] = (c == oldChar) ? newChar : c;
                i++;
            }
            return new String(0, len, buf);
        }
    }
    return this;
}`

I know, the String is immutable and we have a cool string pool, but In this case we have two strong reference, I mean two NEW instance to the same string. Since we have two new instance, it means we have two DIFFERENT memory address.

sources: http://grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java/?v=source

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char,%20char)

narancs
  • 5,234
  • 4
  • 41
  • 60