img Miracle04

TIJ swot_JAVA的初学者(五)

发表于2004/9/16 2:32:00  765人阅读

1.7 Object landscapes and lifetimes

Well, mentioned something about C++

C++ takes the approach that control of efficiency is the most important issue, so it gives programmer the choice to determine the storage and lifetime while the program is being written, by placing the objects on the stack or in the staic storage area. (what the hell is that?) But this approach obviously sacrifices the flexibility.

In Java, objects are created dynamically in a pool of memory called the heap. In this approach, we dont know how many objects we need until the run-time. But because the storage is managed dynamically, at run-time, the amount of time required to allocate storage on the heap is significantly longer than the time to create storage on the stack.

Then , another issue, the lifetime. In a language like C++, it must be determined programmatically when to destroy the object, which can lead to memory leaks if you don't do it correctly. Java provides a feature called garbage collector that automatically discovers when an object is no longer in use and destroys it.

1.7.1 Collections and iterators

In OOP language, we have a set of containers to expand themselves whenever necessary to accomodate everything we place inside it. Such as, in C++ we have Standard Template Library(STL), Object Pascal has Visual Component Library(VCL). In Java, we we standard libraries, like vector, sets, queues, hash tables, trees, stacks, etc.

And, there comes iterator, which is an object as well. Its job is to select the elements within a container and present them to the user of the iterator.

The container, via iterator, is abstracted to be simply a sequence.

In vresion1.0 and 1.1, Java has standard iterator, called Enummeration. Java2 has added a much more complete library that contains an iterator called Iterator.

1.7.2 The singly rooted hierarchy

All classes in Java are inherited from class Object.

Hoop! What do I know?

Singly rooted hierarchy is good for OOP and easier to implement a garbage collctor. Come back later!




0 0



取 消