1) String s = "Hello":
2) String s = new String (" Hello");
In the first statement, assignment operator is used to assign the string literal to the String variable s. In this case, JVM first of all checks whether the same object is already available in the string constant pool. If it is available, then it creates another reference to it. If the same object is not available then it creates another object with the content "Hello" and stores it into the string constant pool.
In the second statement, new operator is used create the string object. In this case, JVM always creates a new object without looking in the string constant pool.
No comments:
Post a Comment