Created
June 22, 2019 14:35
-
-
Save Audhil/14dfccb866c6d02e1bf8fdecd253da1a to your computer and use it in GitHub Desktop.
String is immutable - practical check-up
This file contains hidden or 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
String t = "foo"; // newer obj created and added string pool | |
System.out.println("hashCode of t: " + t.hashCode()); | |
t = "foo"; // shares the same memory location - same hashCode() | |
System.out.println("hashCode of 2 t: " + t.hashCode()); | |
t = "fuc"; // newer value added - newer obj created and added string pool - different hashCode() | |
System.out.println("hashCode of 3 t: " + t.hashCode()); | |
t = new String("foo"); // shares the same memory location - same hashCode() | |
System.out.println("hashCode of 4 t: " + t.hashCode()); | |
t = new StringBuilder("foo").toString(); // shares the same memory location - same hashCode() | |
System.out.println("hashCode of 5 t: " + t.hashCode()); | |
// o/p is | |
hashCode of t: 101574 | |
hashCode of 2 t: 101574 | |
hashCode of 3 t: 101748 | |
hashCode of 4 t: 101574 | |
hashCode of 5 t: 101574 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment