【发布时间】:2018-10-11 09:23:26
【问题描述】:
我有以下测试:
public void testStringReplace()
{
final String placeholder = "$ph$";
final String template = "<test>" + placeholder + "</test>";
final String result = "<test>Hello!</test>";
String copyOfTemplate = template;
copyOfTemplate.replace(placeholder, "Hello!");
if(!copyOfTemplate.equals(result));
fail();
}
测试总是失败,但为什么?我如何定义copyOfTemplate 才能更改它?还是我在这里遗漏了一些其他细节?
【问题讨论】:
-
copyOfTemplate = copyOfTemplate.replace(placeholder, "Hello!");
-
if(!copyOfTemplate.equals(result));去掉末尾的分号!
标签: java string replace immutability final