Java Interview Questions - Strings and Arrays

1. What is the mechanism of string creation and storage?

String s = "abc";//Put "abc" in the constant area, which is generated at compile time
String s = "ab" + "c";//Convert "ab" + "c" to the string constant "abc" and put it in the constant area
String s = new String ("abc");//Put "abc" on the heap at runtime, and create it if "abc" does not exist in the constant pool. Do not create if it already exists
Interview questions:
How many objects does new String("abc") create?

Answer: one or two. If there is "abc" in the constant pool, only one object will be created; if there is no string "abc" in the constant pool, then two objects will be created


 
 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325808185&siteId=291194637