String s = "String";
StringBuffer sb1 = new StringBuffer(s);
StringBuffer sb2 = new StringBuffer(s);
if(sb1==sb2)
{
System.out.println("hi");
}
There is a lot of confusion as far as the string buffers and the strings are concerned. they exhibit lot of peculiar behaviour. can someone explain as to when the two strings will be equal and when tto stringbuffer objects will be equal. I mean under what conditions sometimes doing the same operations on two different set of strings are producing contradicting results. for ex: the code
public class ADirtyOne
{
public static void main(String[] a) {
if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
produces the value "Not Equal" but the same code when changed to the following produces the value
public class ADirtyOne05
{
public static void main(String[] a) {
if("String".replace('g','g') == "String".replace('g','g'))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
"Equal"
I am unable to sort out when there is a distinction and when the strings are equal. are there any articles or examples which can make the concepts of strings and stringbuffers clear. thanks for any help.