Skip to content

Instantly share code, notes, and snippets.

@yakubpashask
Last active September 14, 2019 11:31
Show Gist options
  • Save yakubpashask/92178b597582826053b0c58df877bb3e to your computer and use it in GitHub Desktop.
Save yakubpashask/92178b597582826053b0c58df877bb3e to your computer and use it in GitHub Desktop.
String constant pool and String comparision
public class StringTest{
String a = "a"; // String constant pool
String b = new String("a"); // heap
a==b // not true becuase of different memory location based on the way they created
a.equlas(b) // true
//String b = "a"
StringBuilder builder = new StringBuilder(); // Extends the AbstractStringBuilder class - no thread saftey
StringBuffer buffer = new StringBuffer(); // Extends the AbstractStringBuilder class but all the required methods are synchrnozed so its thread safe
public static void main(String[] args) {
StringTest stringTest = new StringTest();
if(stringTest.a.equals(stringTest.b))
System.out.println("true with equal");
else if(stringTest.a == stringTest.b)
System.out.println("true: with direct comparision ");
else
System.out.println("false");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment