Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String question

 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain me why System.out.println("String".replace('t','T')=="String".replace('t','T')); returns false?
 
Panagiotis Kokolis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prashant Neginahal:
Can anyone explain me why System.out.println("String".replace('t','T')=="String".replace('t','T')); returns false?
 
Panagiotis Kokolis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Panagiotis Kokolis:
[QB][/QB]

Sorry for the empty reply.
a)With == you check address equality not object equality.
Since each new string object goes to a new heap memory position(address) == returns false.
It does not matter if you use the same literal.
b) replace returns a new string object after the replacement is done. This leads us back to point (a).
c) The API docs state:
public String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
 
Panagiotis Kokolis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry again it is the first time I send a reply and I am not used to it yet.
If you want to check object equality use that instead:
System.out.println("String".replace('t','T').equals("String".replace('t','T')));
cheers
 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry!!! Panagiotis Kokolis i did'nt understood ur logic.
Pls look at the fallowing question,
System.out.println("String".replace('t','t')=="String"); It gives true. Why?
According to me , fallowing things will happen,
1) "String" object will be create is STRING-POOL
and replace method will called on that object.
2) Fine, in rt side of == operator , it needs to create one more object, but since it is already created in pool it does'nt go for new creation.
3) so naturally, both operands of == operators point to same memory(that is in string pool)
4) thats why result is true.
If analyse the question that i asked in the same way, it should return me true.
 
Panagiotis Kokolis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace will not return a new string object in this case.
The concept of the constant pool is different from objects residing in the heap memory. Two different string objects may reside in the heap memory and share the same string value which resides in the constant pool.
What follows is the source code of the replace method of the class String from the JDK 1.4.1
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;
}
as you may notice you try to replace 't' with 't'. In this case a new string object will NOT be created. This is why my logic appears to be false.
I hope this helps
Panagiotis
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic