TIJ swot_JAVA的初学者(五)

原创 2004年09月16日 02:32:00

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!





下面介绍一个限制类对象个数的例子: 创建一个名为ConnectionManager的类,

Java相关合集---J2EE EJB TIJ JSP

  • 2010年11月14日 23:24
  • 14.58MB
  • 下载

Thinking in java\TIJ4-solutions-demo

  • 2012年07月28日 00:58
  • 250KB
  • 下载






JAVA的垃圾回收机制是不是可以这么理解: (1)对象所占用的内存空间(通常是经由new创建,在堆中),是由垃圾回收器负责回收;但是这种回收并不一定会发生,JAVA中的对象并不一定会被销毁;内存空间...
  • wl_nj
  • wl_nj
  • 2014年07月22日 09:36
  • 387


什么是吸血鬼数字? 吸血鬼数字是指位数为偶数的数字,可以由一对数字...


知识补充 在讲后面知识之前,我想先补充几个相关的小知识点。                                        Java API文档 我在这里说的第一个就是Java API文...
您举报文章:TIJ swot_JAVA的初学者(五)