版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、<p> 附錄A 外文翻譯—原文部分</p><p> Everything Is an Object</p><p> “If we spoke a different language, we would perceive a some what different world.” Ludwig Wittgenstein (1889-1951)Although it i
2、s based on C++, Java is more of a “pure” object-oriented language.</p><p> 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++
3、. A hybrid language allows multiple programming styles; the reason C++ is hybrid is to support backward compatibility with the C language. Because C++ is a superset of the C language, it includes many of that language’s
4、undesirable features, which can make some aspects of C++ overly complicated.</p><p> The Java language assumes that you want to do only object-oriented programming. This means that before you can begin you
5、must shift your mindset into an object-oriented 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 langua
6、ges. In this chapter you’ll see the basic components of a Java program and learn that (almost) everything in Java is an object.</p><p> You manipulate objects with references</p><p> Each prog
7、ramming language has its own means of manipulating elements in memory. Sometimes the programmer must be constantly aware of what type of manipulation is going on. Are you manipulating the element directly, or are you dea
8、ling with some kind of indirect representation (a pointer in C or C++) that must be treated with a special syntax?</p><p> All this is simplified in Java. You treat everything as an object, using a single c
9、onsistent syntax. Although you treat everything as an object, the identifier you manipulate is actually a “reference” to an object. You might imagine a television (the object) and a remote control 1(the reference). As l
10、ong as you’re holding this reference, you have a connection to the television, but when someone says, “Change the channel” or “Lower the volume,” what you’re manipulating is the reference, which in</p><p>
11、Also, the remote control can stand on its own, with no television. That is, just because you have a reference doesn’t mean there’s necessarily an object connected to it. So if you want to hold a word or sentence, you cre
12、ate a String reference:</p><p> But here you’ve created only the reference, not an object. If you decided to send a message to s at this point, you’ll get an error because s isn’t actually attached to anyth
13、ing (there’s no television). A safer practice, then, is always to initialize a reference when you create it:String s = "asdf";However, this uses a special Java feature: Strings can be initialized with quoted te
14、xt. Normally, you must use a more general type of initialization for objects.</p><p> Where storage lives</p><p> It’s useful to visualize some aspects of how things are laid out while the pro
15、gram is running—in particular how memory is arranged. There are five different places to store data:</p><p> 1. Registers. This is the fastest storage because it exists in a place different from that of ot
16、her storage: inside the processor. However, the number of registers is severely limited, so registers are allocated as they are needed. You don’t have direct control, nor do you see any evidence in your programs that reg
17、isters even exist (C & C++, on the other hand, allow you to suggest register allocation to the compiler).</p><p> 2. The stack. This lives in the general random-access memory (RAM) area, but has direct
18、 support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. This is an extremely fast and efficient way to allocate storage, second only to
19、 registers. The Java system must know, while it is creating the program, the exact lifetime of all the items that are stored on the stack. This constraint places limits on the flexibility of y</p><p> Speci
20、al case: primitive types</p><p> 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 treatme
21、nt 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, inste
22、ad of creating the variable by using new, an “automatic” variable is created that is not a reference. The v</p><p> Java determines the size of each primitive type. These sizes don’t change from one machine
23、 architecture to another as they do in most languages. This size invariance is one reason Java programs are more portable than programs in most other languages.All numeric types are signed, so don’t look for unsigned typ
24、es.The size of the boolean type is not explicitly specified; it is only defined to be able to take the literal values true or false.The “wrapper” classes for the primitive data types allow y</p><p> char c
25、= ‘x’;</p><p> Character ch = new Character(c);</p><p> Or you could also use:</p><p> Character ch = new Character(‘x’);</p><p> Java SE5 autoboxing will automatic
26、ally convert from a primitive to a wrapper type:</p><p> Character ch = ‘x’;</p><p> and back:char c = ch;</p><p> The reasons for wrapping primitives will be shown in a later ch
27、apter.</p><p> High-precision numbers</p><p> Java includes two classes for performing high-precision arithmetic: BigInteger and BigDecimal. Although these approximately fit into the same cate
28、gory as the “wrapper” classes, neither one has a primitive analogue.Both classes have methods that provide analogues for the operations that you perform on primitive types. That is, you can do anything with a BigInteger
29、or BigDecimal that you can with an int or float, it’s just that you must use method calls instead of operators. Also, since there’s mo</p><p> BigInteger supports arbitrary-precision integers. This means th
30、at you can accurately represent integral values of any size without losing any information during operations.</p><p> BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurat
31、e monetary calculations, for example.</p><p> Consult the JDK documentation for details about the constructors and methods you can call for these two classes.</p><p> You never need todestroy
32、an object</p><p> In most programming languages, the concept of the lifetime of a variable occupies a significant portion of the programming effort. How long does the variable last? If you are supposed to d
33、estroy it, when should you? Confusion over variable lifetimes can lead to a lot of bugs, and this section shows how Java greatly simplifies the issue by doing all the cleanup work for you.</p><p> 附錄B 外文翻譯—
34、譯文部分</p><p><b> 一切都是對象</b></p><p> “如果我們說另一種不同的語言,那么我們就會發(fā)覺一個有些不同的世界”。</p><p> —Ludwig Wittgenstein(1889-1951)</p><p> “盡管以C++為基礎,但Java是一種更純粹的面向?qū)ο蟪绦蛟O計語言”
35、。</p><p> 無論C++還是Java都屬于雜合語言。但在Java中,設計者覺得這種雜合并不像在C++里那么重要。雜合語言允許采用多種編程風格;之所以說C++是一種雜合語言,是因為它支持與C語言的向后兼容能力。由于C++是C的一個超集,所以包含的許多特性都是后者不具備的,這些特性使C++在某些地方顯得過于復雜。</p><p> Java語言首先便假定了我們只希望進行面向?qū)ο蟮某?/p>
36、序設計。也就是說,正式用它設計之前,必須先將自己的思想轉(zhuǎn)入一個面向?qū)ο蟮氖澜纾ǔ窃缫蚜晳T了這個世界的思維方式)。只有做好這個準備工作,與其他OOP語言相比,才能體會到Java的易學易用。在本章,我們將探討Java程序的基本組件,并體會為什么說Java乃至Java程序內(nèi)的一切都是對象。</p><p><b> 用句柄操縱對象</b></p><p> 每種編程語
37、言都有自己的數(shù)據(jù)處理方式。有些時候,程序員必須時刻留意準備處理的是什么類型。您曾利用一些特殊語法直接操作過對象,或處理過一些間接表示的對象嗎(C或C++里的指針)?</p><p> 所有這些在Java里都得到了簡化,任何東西都可看作對象,。因此,我們可采用一種統(tǒng)一的語法,任何地方均可照搬不誤。但要注意,盡管一切都“看作”對象,但操縱的標識符實際是指向一個對象的“句柄”(Handle)。在其他Java參考書里,
38、還可看到有的人將其稱作一個“引用”,甚至一個“指針”??蓪⑦@一情形想象成用遙控板(句柄)操縱電視機(對象)。只要握住這個遙控板,就相當于掌握了與電視機連接的通道。但一旦需要“換頻道”或者“關小聲音”,我們實際操縱的是遙控板(句柄),再有遙控板自己操縱電視機(對象)。如果要在房間里四處走走,并想保持對電視機的控制,那么手上拿著的是遙控板,而非電視機。</p><p> 此外,即使沒有電視機,遙控板亦可獨立存在。也
39、就是說,只是由于擁有一個句柄,并不表示必須有一個對象同它連接。所以如果想容納一個詞或句子,可創(chuàng)建一個String句柄:</p><p><b> String s;</b></p><p> 但這里創(chuàng)建的只是句柄,并不是對象。若此時向s發(fā)送一條消息,就會獲得一個錯誤(運行期)。</p><p> 這是由于s實際并未與任何東西連接(即“沒有
40、電視機”)。因此,一種更安全的做法是:創(chuàng)建一個句柄時,記住無論如何都進行初始化:</p><p> String s = “zyp”;</p><p> 然而,這里用到了Java語言的一個特性:字串可以用加引號的文本初始化。通常,必須為對象使用一種更通用的初始化方法。</p><p><b> 存儲到什么地方</b></p>
41、<p> 程序運行時,我們最好對數(shù)據(jù)保存到什么地方做到心中有數(shù)。特別要注意的是內(nèi)存的分配。有六個地方都可以保存數(shù)據(jù):</p><p> ?。?)寄存器。這是最快的保存區(qū)域,因為它位于不同于其他存儲區(qū)的地方——處理器內(nèi)部。然而,寄存器的數(shù)量十分有限,所以寄存器是根據(jù)需要由編譯器分配。我們對此沒有直接的控制權(quán),也不可能在自己的程序里找到寄存器存在的任何蹤跡(另一方面,C和C++允許您向編譯器建議寄存器的
42、分配方式)。</p><p> ?。?)堆棧。駐留于常規(guī)RAM(隨機訪問存儲器)區(qū)域,但可通過它的“堆棧指針”獲得處理的直接支持。堆棧指針若向下移,會創(chuàng)建新的內(nèi)存;若向上移,則會釋放那些內(nèi)存。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存器。創(chuàng)建程序時,Java編譯器必須準確地知道堆棧內(nèi)保存的所有數(shù)據(jù)的“長度”以及“存在時間”。這是由于它必須生成相應的代碼,以便向上和向下移動指針。這一限制無疑影響了程序的靈活
43、性,所以盡管有些Java數(shù)據(jù)要保存在堆棧里——特別是對象句柄,但Java對象并不存儲于其中。</p><p> ?。?)堆。一種通用的內(nèi)存池(也在RAM區(qū)域),用于存放所有的Java對象。和堆棧不同,“內(nèi)存堆”或“堆”(Heap)最吸引人的地方在于編譯器不必知道要從堆里分配多少存儲空間,也不必知道存儲的數(shù)據(jù)要在堆里停留多長時間。因此,用堆保存數(shù)據(jù)時會得到更大的靈活性。要求創(chuàng)建一個對象時,只需要用new命令編制相關
44、的代碼即可。執(zhí)行這些代碼時,會在堆里自動進行數(shù)據(jù)的保存。當然,為達到這種靈活性,必然付出一定的代價:在堆里分配存儲空間時會花掉更長的時間(如果確實可以在Java中像在C++中一樣在棧中創(chuàng)建對象)。</p><p> (4)常量存儲。常數(shù)值通常直接置于程序代碼內(nèi)部。這樣做是安全的,因為他們永遠都不會改變。有的常數(shù)需要嚴格地保護,所以可考慮將他們置入只讀存儲器(ROM)。</p><p>
45、?。?)非RAM存儲。若數(shù)據(jù)完全獨立于一個程序之外,則程序不運行時仍可存在,并在程序的控制范圍之外。其中兩個最重要的例子便是“流式對象”和“持久化對象”。對于流式對象,對象會轉(zhuǎn)化成字節(jié)流,通常會發(fā)給另一臺機器。而對于持久化對象,對象保存在磁盤中。即使程序中止運行,他們?nèi)钥杀3肿约旱臓顟B(tài)不變。對于這些類型的數(shù)據(jù)存儲,一個特別有用的技巧就是它們能存在于其他媒體中。一旦需要,甚至能將他們恢復成普通的、基于RAM的對象。Java 提供了對輕量級
46、持久化的支持,而諸如JDBC和Hibernate這樣的機制提供了更加復雜的對數(shù)據(jù)庫中存儲和讀取對象信息的支持。</p><p><b> 特例:基本類型</b></p><p> 在程序設計中經(jīng)常用到一系列類型,他們需要特殊對待。可以把他們想像成“基本”類型。之所以特殊對待,是因為new將對象存儲在“堆”里,故用new創(chuàng)建一個對象——特別是小的、簡單的變量,往往不
47、是很有效。因此,對于這些類型,Java采取與C和C++相同的方法。也就是說,不用new來創(chuàng)建變量,而是創(chuàng)建一個并非是引用的“自動”變量。這個變量直接存儲“值”,并置于堆棧中,因此更加高效。</p><p> Java要確定每種基本類型所占存儲空間的大小。他們的大小并不像其他大多數(shù)語言那樣隨機器硬件架構(gòu)的變化而變化。這種所占存儲空間大小的不變性是Java程序比用其他大多數(shù)語言編寫的程序更具可移植性的原因。所有數(shù)值
48、類型都有正負號,所以不要去尋找無符號的數(shù)值類型。Boolean類型所占存儲空間的大小沒有明確指定,僅定義為能夠取字面值true或false?;绢愋途哂械陌b器類,使得可以在堆中創(chuàng)建一個非基本對象,用來表示對應的基本類型。例如:</p><p> Char c = ‘x’;</p><p> Character ch = new Character(c);</p><
49、;p><b> 也可以這樣用:</b></p><p> Character ch = new Character(‘x’);</p><p> 包裝基本類型的原因?qū)⒃谝院蟮恼鹿?jié)中說明。</p><p><b> 高精度數(shù)字</b></p><p> Java提供了兩個用于高精度計算
50、的類:BigInteger 或 BigDecimal。雖然它們大體上屬于“包裝器類”的范疇,但二者都沒有對應的基本類型。</p><p> 不過,這兩個類包含的方法,提供的操作與對基本類型所能執(zhí)行的操作相識。也就是說,能作用于int或float的操作,也能作用于BigInteger或Big Decimal。</p><p> 只不過必須以方法調(diào)用方式取代運算符方式來實現(xiàn)。由于這么做復雜
51、了許多,所以運算速度會比較慢。在這里,我們以速度換取了精度。</p><p> BigInteger支持任意精度的整數(shù)。也就是說,在運算中,可以準確地表示任何大小的整數(shù)值,而不會丟失任何信息。</p><p> BigDecimal支持任何精度的定點數(shù),例如,可以用它進行精確的貨幣計算。</p><p> 關于調(diào)用這兩個類的構(gòu)造器和方法的詳細信息,請查閱JDK
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 面向?qū)ο笤O計外文翻譯 (2)
- 面向?qū)ο蟪绦蛟O計外文翻譯2
- 面向?qū)ο笾型馕姆g
- java的面向?qū)ο缶幊掏馕馁Y料翻譯
- 面向?qū)ο笤O計
- 面向?qū)ο笤O計類和對象
- 外文翻譯---面向?qū)ο蟮囊簤合到y(tǒng)分析研究
- 網(wǎng)頁設計外文翻譯---使用xmlhttprequest對象
- 外文翻譯-對象技術(shù)
- 面向?qū)ο笤O計原則
- 計算機畢業(yè)論文外文翻譯---面向?qū)ο蠛蚦++
- 計算機系畢業(yè)設計外文翻譯---面向?qū)ο髷?shù)據(jù)庫系統(tǒng)
- 外文翻譯-機械模具自動化【期刊】支持機床系統(tǒng)的面向?qū)ο笤O計-中英全
- 軟件數(shù)據(jù)庫的面向?qū)ο蟮囊暯峭馕奈墨I翻譯
- 《面向?qū)ο蟪绦蛟O計》
- 《面向?qū)ο蟪绦蛟O計(java)》
- 面向?qū)ο笳n程設計報告
- 面向?qū)ο蟮姆治龊驮O計
評論
0/150
提交評論