Last active
September 14, 2019 11:31
-
-
Save yakubpashask/92178b597582826053b0c58df877bb3e to your computer and use it in GitHub Desktop.
String constant pool and String comparision
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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