jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String question" Watch "String question" New topic
Author

String question

Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
Can anyone explain me why System.out.println("String".replace('t','T')=="String".replace('t','T')); returns false?


--------------<br />Prashant<br />SCJP-91%
Panagiotis Kokolis
Greenhorn

Joined: Nov 22, 2002
Posts: 7
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

Joined: Nov 22, 2002
Posts: 7
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

Joined: Nov 22, 2002
Posts: 7
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

Joined: Dec 04, 2002
Posts: 76
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

Joined: Nov 22, 2002
Posts: 7
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
 
Consider Paul's rocket mass heater.
 
subject: String question
 
Similar Threads
Generics method declaration confusion. Plz clarify
Interesting string methods
String literals
string question
Java Strings