【正文】
n it does to allocate stack storage (if you even could create objects on the stack in Java, as you can in C++). 4. Static storage. “Static” is used here in the sense of “in a fixed location” (although it?s also in RAM). Static storage contains data that is available for the entire time a program is running. You can use the static keyword to specify that a particular element of an object is static, but Java objects themselves are never placed in static storage. 5. Constant storage. Constant values are often placed directly in the program code, which is safe since they can never change. Sometimes constants are cordoned off by themselves so that they can be optionally placed in readonly memory (ROM), in embedded systems. 6. NonRAM storage. If data lives pletely outside a program, it can exist while the program is not running, outside the control of the program. The two primary examples of this are streamed objects, in which objects are turned into streams of bytes, generally to be sent to another machine, and persistent objects, in which the objects are placed on disk so they will hold their state even when the program is terminated. The trick with these types of storage is turning the objects into something that can exist on the other medium, and yet can be resurrected into a regular RAMbased object when necessary. Java provides support for lightweight persistence, and future versions of Java might provide more plete solutions for persistence. Special case: primitive types One group of types, which you?ll use quite often in your programming, gets special treatment. You can think of these as “primitive” very efficient, because new places objects on the heap. For these types Java falls back on the approach taken by C and C++. That is, instead of creating the variable by using new, an “automatic” variable is created that is not a reference. The variable holds the value, and it?s placed on the stack, so it?s much more efficient. Java determines the size of each primitive type. These sizes don?t change from one machine architecture to another as they do in most languages. This size invariance is one reason Java programs are portable. All numeric types are signed, so don?t look for unsigned types. The size of the boolean type is not explicitly specified。所以假若使用下面這段代碼: 附件 2:外文原文 (復(fù)印件 ) Everything is an Object Although it is based on C++, Java is more of a “pure” objectoriented language. Both C++ and Java are hybrid languages, but in Java the designers felt that the hybridization was not as important as it was in C++. A hybrid langua ge allows multiple programming styles。所以 C 和 C++能將一個變量“隱藏”在一個更大的作用域里。由于 Java 是一種形式自由的語言,所以額外的空格、制表位以及回車都不會對結(jié)果程序造成影響。參考下面這個例子: { int x = 12。 7. 絕對不要清除對象 在大多數(shù)程 如何幫助我們完成所有清除工作,從而極大了簡化了這個問題。因此,典型的數(shù)組錯誤在 Java 里就得到了避免。而且每個 句柄都會自動初始化成一個特殊值,并帶有自己的關(guān)鍵字: null(空)。由于系統(tǒng)自動進行范圍檢查,所以必然要付出一些代價:針對每個數(shù)組,以及在運行期間對索引的校驗,都會造成少量的內(nèi)存開銷。在 C++里,應(yīng)盡量不要使用數(shù)組,換用標準模板庫( Standard TemplateLibrary)里更安全的容器。 至于調(diào)用這兩個類時可選用的構(gòu)建器和方法,請自行參考聯(lián)機幫助文檔。 BigInteger 支持任意精度的整數(shù)。也就是說,能對 int 或 float 做的事情,對 BigInteger 和 BigDecimal 一樣可以做。)。c39。這意味著假如想讓堆內(nèi)一個非主要對象表示那個主類型,就要使用對應(yīng)的封裝器。就象在大多數(shù)語言里那樣,這些大小并不隨著機器結(jié)構(gòu)的變化而變化。之所以要特別對待,是由于用 new 創(chuàng)建對象(特別是小的、簡單的變量)并不是非常有效,因為 new 將對象置于“堆”里。一旦需要,甚至能將它們恢復(fù)成普通的、基于 RAM的對象。對于流式對象,對象會變成字節(jié)流,通常會發(fā)給另一臺機器。有的常數(shù)需要嚴格地保護,所以可考慮將它們置入只讀存儲器( ROM)。但 Java 對象本身永遠都不會置入靜態(tài)存儲空間。當然,為達到這種靈活性,必然會付出一定的 代價:在堆里分配存儲空間時會花掉更長的時間! (4) 靜態(tài)存儲。 (3) 堆。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存器。我們對此沒有直接的控制權(quán),也不可能在自己的程序里找到寄存器存在的任何蹤跡。特別要注意的是內(nèi)存的分配。 Java 配套提供了數(shù)量眾多的現(xiàn)成類型。new的意思是:“把我變成這些對象的一種新類型”。然而