【正文】
執(zhí)行這些代碼時,會在堆里自動進行數(shù)據(jù)的保存。有六個地方都可以保存數(shù)據(jù): (1) 寄存器。若此時向 s發(fā)送一條消息,就會獲得一個錯誤(運行期)。也就是說,正式用它設計之前,必須先將自己的思想轉(zhuǎn)入一個面向 對象的世界(除非早已習慣了這個世界的思維方式)。只有做好這個準備工作,與其他 OOP 語言相比,才能體會到 Java 的易學易用。這是由于 s實際并未與任何東西連接(即 “ 沒有電視機 ” )。這是最快的保存區(qū)域,因為它位于和其他所有保存方式不同的地方:處理器內(nèi)部。當然,為達到這種靈活性,必然會付出一的代價:在堆里分配存儲空間時會花掉更長的時間! 第 7 頁 共 7 頁 (4) 靜態(tài)存儲。一旦需要,甚至能將它們恢復成普通的、基于 RAM 的對象。也就是說,不是用 new 創(chuàng)建變量,而是創(chuàng)建一個并非句柄的 “ 自動 ” 變量。常數(shù)值通常直接置于程序代碼內(nèi)部。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存 器。 new的意思是: “ 把我變成這些對象的一種新類型 ” 。但要注意,盡管將一切都 “ 看作 ” 對象,但操縱的標識符實際是指向一個對象的 “ 句柄 ” ( Handle)。 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 you’re manipulating is the reference, which in turn modifies the object. If you want to move around the room and still control the television, you tak