【正文】
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++能將一個(gè)變量“隱藏”在一個(gè)更大的作用域里。由于 Java 是一種形式自由的語言,所以額外的空格、制表位以及回車都不會(huì)對結(jié)果程序造成影響。參考下面這個(gè)例子: { int x = 12。 7. 絕對不要清除對象 在大多數(shù)程 如何幫助我們完成所有清除工作,從而極大了簡化了這個(gè)問題。因此,典型的數(shù)組錯(cuò)誤在 Java 里就得到了避免。而且每個(gè) 句柄都會(huì)自動(dòng)初始化成一個(gè)特殊值,并帶有自己的關(guān)鍵字: null(空)。由于系統(tǒng)自動(dòng)進(jìn)行范圍檢查,所以必然要付出一些代價(jià):針對每個(gè)數(shù)組,以及在運(yùn)行期間對索引的校驗(yàn),都會(huì)造成少量的內(nèi)存開銷。在 C++里,應(yīng)盡量不要使用數(shù)組,換用標(biāo)準(zhǔn)模板庫( Standard TemplateLibrary)里更安全的容器。 至于調(diào)用這兩個(gè)類時(shí)可選用的構(gòu)建器和方法,請自行參考聯(lián)機(jī)幫助文檔。 BigInteger 支持任意精度的整數(shù)。也就是說,能對 int 或 float 做的事情,對 BigInteger 和 BigDecimal 一樣可以做。)。c39。這意味著假如想讓堆內(nèi)一個(gè)非主要對象表示那個(gè)主類型,就要使用對應(yīng)的封裝器。就象在大多數(shù)語言里那樣,這些大小并不隨著機(jī)器結(jié)構(gòu)的變化而變化。之所以要特別對待,是由于用 new 創(chuàng)建對象(特別是小的、簡單的變量)并不是非常有效,因?yàn)?new 將對象置于“堆”里。一旦需要,甚至能將它們恢復(fù)成普通的、基于 RAM的對象。對于流式對象,對象會(huì)變成字節(jié)流,通常會(huì)發(fā)給另一臺(tái)機(jī)器。有的常數(shù)需要嚴(yán)格地保護(hù),所以可考慮將它們置入只讀存儲(chǔ)器( ROM)。但 Java 對象本身永遠(yuǎn)都不會(huì)置入靜態(tài)存儲(chǔ)空間。當(dāng)然,為達(dá)到這種靈活性,必然會(huì)付出一定的 代價(jià):在堆里分配存儲(chǔ)空間時(shí)會(huì)花掉更長的時(shí)間! (4) 靜態(tài)存儲(chǔ)。 (3) 堆。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存器。我們對此沒有直接的控制權(quán),也不可能在自己的程序里找到寄存器存在的任何蹤跡。特別要注意的是內(nèi)存的分配。 Java 配套提供了數(shù)量眾多的現(xiàn)成類型。new的意思是:“把我變成這些對象的一種新類型”。然而