Discussion about java object

There is a significant difference with C ++ in java.
String AK = new String ();
statement Ak is a new object. On the storage area it is different. Written in C ++ String Ak; it will take the initiative assigns a random value. Variables are not allowed in java is not initialized, the program will complain. And only the space occupied by such a statement is the stack, you can not call an object method (c ++ in function). After declaring the space occupied by the object heap, and the method String () object can be called.
In the example cited a saw;
when we use when String str = 'str', will find first-in memory there is no 'str' object, if not new String ( 'str'); if there is then a str pointing to the object on it.
Will be used when the new String str = new String ( 'str ') directly in a memory 'str' object even have no memory. If the memory already has 'str' object, and then there are two.

Guess you like

Origin www.cnblogs.com/sk244/p/12380823.html