【正文】
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 One group of types, which you’ll use quite often in your programming, gets special treatment. You can think of these as “primitive” types. The reason for the special treatment is that to create an object with new—especially a small, simple variable—isn’t 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 Java 編程思想 第 5 頁 共 7 頁 “ 盡管以 C++為基礎(chǔ),但 Java 是一種更純粹的面向?qū)ο蟪绦蛟O(shè)計(jì)語言 ” 。無論 C++還是 Java 都屬于雜合語言。 Java 語言首先便假定了我們只希望進(jìn)行面向?qū)ο蟮某绦蛟O(shè)計(jì)。 每種編程語言都有自己的數(shù)據(jù)處理方式。但要注意,盡管將一切都 “ 看作 ” 對象,但操縱的標(biāo)識符實(shí)際是指向一個(gè)對象的 “ 句柄 ” ( Handle)。但一旦需要 “ 換頻道 ” 或者 “ 關(guān)小聲音 ” ,我們實(shí)際操縱的是遙控板(句柄),再由遙控板自己操縱電視機(jī)(對象)。所以如果想容納一個(gè)詞或句子,可創(chuàng)建一個(gè) String句柄: 但這里創(chuàng)建的只是句柄,并不是對象。 然而,這里采用的是一種特殊類型:字串可用加引號的文字初始化。 new的意思是: “ 把我變成這些對象的一種新類型 ” 。 Java 配套提供了數(shù)量眾多的現(xiàn)成類型。特別要注意的是內(nèi)存的分配。我們對此沒有直接的控制權(quán),也不可能在自己的程序里找到寄存器存在的任何蹤跡。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存 器。 (3) 堆。要求創(chuàng)建一個(gè)對象時(shí),只需用 new命令編制相關(guān)的代碼即可。程序運(yùn)行期間,靜態(tài)存儲(chǔ)的數(shù)據(jù)將隨時(shí)等候調(diào)用。常數(shù)值通常直接置于程序代碼內(nèi)部。若數(shù)據(jù)完全獨(dú)立于一個(gè)程序之外,則程序不運(yùn)行時(shí)仍可存在,并在程序的控制范圍之外。即使程序中止運(yùn)行,它們?nèi)钥杀3肿约旱臓顟B(tài)不變。未來的版本甚至可能提供更完整的方案。也就是說,不是用 new 創(chuàng)建變量,而是創(chuàng)建一個(gè)并非句柄的 “ 自動(dòng) ” 變量。這種大小的不可更改正是 Java 程序具有很強(qiáng)移植能力的原因之一。 Java 決定了 每種主要類型的大小。之所以要特別對待,是由于用new創(chuàng)建對象(特別是小的、簡單的變量)并不是非常有效,因?yàn)?new 將對象置于 “