【正文】
). A safer practice, then, is always to initialize a reference when you create it. 第 3 頁 共 7 頁 However, this uses a special Java feature: strings can be initialized with quoted text. Normally, you must use a more general type of initialization for objects When you create a reference, you want to connect it with a new object. You do so, in general, with the new keyword. The keyword new says, “Make me a new one of these objects.” So in the preceding example, you can say: Not only does this mean “Make me a new String,” but it also gives information about how to make the String by supplying an initial character string. Of course, String is not the only type that exists. Java es with a plethora of readymade types. What’s more important is that you can create your own types. In fact, that’s the fundamental activity in Java programming, and it’s what you’ll be learning about in the rest of this book It’s useful to visualize some aspects of how things are laid out while the program is running—in particular how memory is arranged. There are six different places to store data: Registers. This is the fastest storage because it exists in a place different from that of other storage: inside the processor. However, the number of registers is severely limited, so registers are allocated by the piler according to its needs. You don’t have direct control, nor do you see any evidence in your programs that registers even exist. The stack. This lives in the general randomaccess memory (RAM) area, but has direct support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. This is an extremely fast and efficient way to allocate storage, second only to registers. The Java piler must know, while it is creating the program, the exact size and lifetime of all the data that is stored on the stack, because it must generate the code to move the stack pointer up and down. This constraint places limits on the flexibility of your programs, so while some Java storage exists on the stack—in particular, object references—Java objects themselves are not placed on the stack. The heap. This