
Originally Posted by
Dilenger4
Java treats string of the same content to be of the same ref.
False
make a test program and do this
Code:
String lsString1 = new String ("Hi");
String lsString2 = new String ("Hi");
if (lsString1 == lsString2)
{
JOptionPane.showMessageDialog(null, "lsString1 == lsString2");
}
if (lsString1.compareTo(lsString2) == 0)
{
JOptionPane.showMessageDialog(null, "lsString1 compare to lsString2 = 0");
}
result: only the MessageBox called by the compareTo will be displayed because lsString1 == lsString2 is false. The object reference is compared and it is not the same even if lsString1 is the same text as lsString2. By using compareTo, it compares the text itself and not the reference.
(I learned that in my java class)
that's why I said use the StringBuffer or there will be too much unuseful String objects.
I hope didn't make that much mistakes!