In two ways java String initialization

String str=new String("XXX"); 
String str="XXX";    

Both look the same, in fact, very different. 
 The former is a java standard way to create objects, objects that it creates will be placed directly into the heap, one creates a new object for each call; the latter will create an object on the stack reference variable str, and then view the stack the existence of "XXX", if not, the "XXX" is stored into the stack, and to make reference variable str to it; if you already have "XXX", is the direct cause of str point to it. Such full use of the advantages of shared data stack, of course, can also be a trap, it is not likely to create an object, just point to an object has been previously created; and new () method is to ensure that every time a new object is created .

Guess you like

Origin blog.csdn.net/abc_123456___/article/details/90706141