- 论坛徽章:
- 0
|
java中的new,希望高手帮助解答一下(急)
如果没有用new, i.e String s; 只是declare了一个reference which is a type of String. There is no new object is created or there is no memory allocation in heap.
To create a new String object, you have to use keyword new i.e. String s = new String("a". There is memory allocated in heap for the new object, and the 'entry' to the object in heap is assigned to the reference 's'.
Before you use any reference, you have to initiate it by either referring it to an existing object or creating a new object for it. If you don't, the reference is useless, your program will not compile or RuntimeException will be thown. |
|