In Java, is there a difference between the following two pieces of code? I’m looking for answers in terms of memory usage and the String pool.
The first:
String s = new String();
s = "abcdef";
The second:
String s = new String("abcdef");
Thanks.
You do a creation and a value assignment in the first one. In the second one you just do a creation. You make (nearly) twice processor activities in the first one. Speaking of memory, there’s no difference.
And String pool explanation to your question:
What is the Java string pool and how is "s" different from new String("s")?