【正文】
就象在大多數(shù)語言里那樣,這些大小并不隨著機(jī)器結(jié)構(gòu)的變化而變化。對于這些類型, Java 采納了與 C 和 C++相同的方法。 Java 提供了對 Lightweight persistence 的支持。而對于固定對象,對象保存在磁盤中。 (6) 非 RAM 存儲(chǔ)。 (5) 常數(shù)存儲(chǔ)。這兒的 “ 靜態(tài) ” ( Static)是指 “ 位于固定位置 ” (盡管也在 RAM里)。因此,用堆保存數(shù)據(jù)時(shí)會(huì)得到更大的靈 活性。這一限制無疑影響了程序的靈活性,所以盡管有些 Java 數(shù)據(jù)要保存在堆棧里 —— 特別是對象句柄,但 Java 對象并不放到其中。堆棧指針若向下移,會(huì)創(chuàng)建新的內(nèi)存;若向上移,則會(huì)釋放那些內(nèi)存。然而,寄存器的數(shù)量十分有限,所以寄存器是根據(jù)需要由編譯器分配。 程序運(yùn)行時(shí),我們 最好對數(shù)據(jù)保存到什么地方做到心中有數(shù)。 當(dāng)然,字串( String)并非唯一的類型。通常用 new 關(guān)鍵字達(dá)到這一目的。因此,一種 更安全的做法是:創(chuàng)建一個(gè)句柄時(shí),記住無論如何都進(jìn)行初始化: 。也就是說,只是由于擁有一個(gè)句柄,并不表示必須有一個(gè)對象同它連接。只要握住這個(gè)遙控板,就相當(dāng)于掌握了與電視機(jī)連接的通道。因此,我們可采用一種統(tǒng)一的語法,任何地方均 可照搬不誤。在本章,我們將探討 Java 程序的基本組件,并體會(huì)為什么說 Java 乃至 Java 程序內(nèi)的一切都是對象。由于 C++是 C 的一個(gè)超集,所以包含的許多特性都是后者不具備的,這些特性使 C++在某些地方顯得過于復(fù)雜。 the reason C++ is hybrid is to support backward patibility with the C language. Because C++ is a superset of the C language, it includes many of that language’s undesirable features, which can make some aspects of C++ overly plicated. The Java language assumes that you want to do only objectoriented programming. This means that before you can begin you must shift your mindset into an objectoriented world (unless it’s already there). The benefit of this initial effort is the ability to program in a language that is simpler to learn and to use than many other OOP languages. In this chapter we’ll see the basic ponents of a Java program and we’ll learn that everything in Java is an object, even a Java program. Each programming language has its own means of manipulating data. Sometimes the programmer must be constantly aware of what type of manipulation is going on. Are you manipulating the object directly, or are you dealing with some kind of indirect representation (a pointer in C or C++) that must be treated with a special syntax? All this is simplified in Java. You treat everything as an object, using a single consistent syntax. Although you treat everything as an object, the identifier you manipulate is actually a “reference” to an object. You might imagine this scene as a television (the object) with your remote control (the reference). As long as you’re holding this reference, you have a connection to the television, but when someone says “change the channel” or “l(fā)ower the volume,” what