2023年全國碩士研究生考試考研英語一試題真題(含答案詳解+作文范文)_第1頁
已閱讀1頁,還剩9頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、<p><b>  英文原文</b></p><p>  Everything is an Object</p><p>  Although it is based on C++, Java is more of a “pure” object-oriented language.</p><p>  Both C++ and Ja

2、va are hybrid languages, but in Java the designers felt that the hybridization was not as important as it was in C++. A hybrid language allows multiple programming styles; the reason C++ is hybrid is to support backward

3、compatibility 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 complicated. </p><p>  The

4、Java language assumes that you want to do only object-oriented programming. This means that before you can begin you must shift your mindset into an object-oriented world (unless it’s already there). The benefit of this

5、initial effort is the ability to program in a language that is simpler to learn and to use than many other OOP languages. Alown we’ll see the basic components of a Java program and we’ll learn that everything in Java is

6、an object, even a Java program. </p><p>  You manipulate objects with references </p><p>  Each programming language has its own means of manipulating data. Sometimes the programmer must be cons

7、tantly 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 sy

8、ntax? 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 manipul</p><p><b>  String s;&l

9、t;/b></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 (at run time) because s isn’t actually attached to anythi

10、ng (there’s no television). A safer practice, then, is always to initialize a reference when you create it: </p><p>  String s = "asdf";</p><p>  However, this uses a special Java feat

11、ure: strings can be initialized with quoted text. Normally, you must use a more general type of initialization for objects. </p><p>  You must create all the objects</p><p>  When you create a r

12、eference, you want to connect it with a new object. You do so, in general, with the new keyword. The keyword new says, “Make me a new one of these objects.” So in the preceding example, you can say: </p><p>

13、  String s = new String("asdf");</p><p>  Not only does this mean “Make me a new String,” but it also gives information about how to make the String by supplying an initial character string. Of cou

14、rse, String is not the only type that exists. Java comes with a plethora of ready-made types. What’s more important is that you can create your own types. In fact, that’s the fundamental activity in Java programming, and

15、 it’s what you’ll be learning about in the rest of this book. </p><p>  It’s useful to visualize some aspects of how things are laid out while the program is running—in particular how memory is arranged. The

16、re are six different places to store data: </p><p>  Registers. This is the fastest storage because it exists in a place different from that of other storage: inside the processor. However, the number of reg

17、isters is severely limited, so registers are allocated by the compiler according to its needs. You don’t have direct control, nor do you see any evidence in your programs that registers even exist. </p><p> 

18、 The stack. This lives in the general random-access memory (RAM) area, but has direct support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that me

19、mory. This is an extremely fast and efficient way to allocate storage, second only to registers. The Java compiler must know, while it is creating the program, the exact size and lifetime of all the data that is stored o

20、n the stack, because it must generate the code to move the st</p><p>  The heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. The nice thing about the heap is

21、that, unlike the stack, the compiler doesn’t need to know how much storage it needs to allocate from the heap or how long that storage must stay on the heap. Thus, there’s a great deal of flexibility in using storage on

22、the heap. Whenever you need to create an object, you simply write the code to create it by using new, and the storage is allocated on the heap w</p><p>  Static storage. “Static” is used here in the sense of

23、 “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 objec

24、t is static, but Java objects themselves are never placed in static storage. </p><p>  Constant storage. Constant values are often placed directly in the program code, which is safe since they can never chan

25、ge. Sometimes constants are cordoned off by themselves so that they can be optionally placed in read-only memory (ROM), in embedded systems. </p><p>  Non-RAM storage. If data lives completely outside a prog

26、ram, 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 an

27、other 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 somethin

28、g th</p><p>  High-precision numbers</p><p>  Java includes two classes for performing high-precision arithmetic: Big Integer and Big Decimal. Although these approximately fit into the same cate

29、gory as the “wrapper” classes, neither one has a primitive analogue. </p><p>  Both classes have methods that provide analogues for the operations that you perform on primitive types. That is, you can do any

30、thing with a Big Integer or Big Decimal that you can with an int or float, it’s just that you must use method calls instead of operators. Also, since there’s more involved, the operations will be slower. You’re exchangin

31、g speed for accuracy. </p><p>  Big Integer supports arbitrary-precision integers. This means that you can accurately represent integral values of any size without losing any information during operations. &

32、lt;/p><p>  Big Decimal is for arbitrary-precision fixed-point numbers; you can use these for accurate monetary calculations, for example. </p><p>  Consult the JDK documentation for details about

33、the constructors and methods you can call for these two classes. </p><p>  Arrays in Java</p><p>  Virtually all programming languages support arrays. Using arrays in C and C++ is perilous becau

34、se those arrays are only blocks of memory. If a program accesses the array outside of its memory block or uses the memory before initialization (common programming errors), there will be unpredictable results. </p>

35、<p>  One of the primary goals of Java is safety, so many of the problems that plague programmers in C and C++ are not repeated in Java. A Java array is guaranteed to be initialized and cannot be accessed outside

36、of its range. The range checking comes at the price of having a small amount of memory overhead on each array as well as verifying the index at run time, but the assumption is that the safety and increased productivity i

37、s worth the expense. </p><p>  When you create an array of objects, you are really creating an array of references, and each of those references is automatically initialized to a special value with its own k

38、eyword: null. When Java sees null, it recognizes that the reference in question isn’t pointing to an object. You must assign an object to each reference before you use it, and if you try to use a reference that’s still n

39、ull。Thus, typical array errors are prevented in Java. </p><p>  You can also create an array of primitives. Again, the compiler guarantees initialization because it zeroes the memory for that array. </p&g

40、t;<p>  You never need to destroy 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 th

41、e variable last? If you are supposed to destroy 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

42、 for you. </p><p><b>  Scoping</b></p><p>  Most procedural languages have the concept of scope. This determines both the visibility and lifetime of the names defined within that sco

43、pe. In C, C++, and Java, scope is determined by the placement of curly braces. </p><p>  A variable defined within a scope is available only to the end of that scope. </p><p>  Any text after a

44、‘//’ to the end of a line is a comment. </p><p>  Indentation makes Java code easier to read. Since Java is a free-form language, the extra spaces, tabs, and carriage returns do not affect the resulting prog

45、ram. </p><p>  The compiler will announce that the variable x has already been defined. Thus the C and C++ ability to “hide” a variable in a larger scope is not allowed.</p><p>  Scope of object

46、s</p><p>  Java objects do not have the same lifetimes as primitives. When you create a Java object using new, it hangs around past the end of the scope. </p><p>  the references vanishes at the

47、 end of the scope. However, the String object that s was pointing to is still occupying memory. In this bit of code, there is no way to access the object, because the only reference to it is out of scope. In later chapte

48、rs you’ll see how the reference to the object can be passed around and duplicated during the course of a program. </p><p>  It turns out that because objects created with new stay around for as long as you w

49、ant them, a whole slew of C++ programming problems simply vanish in Java. The hardest problems seem to occur in C++ because you don’t get any help from the language in making sure that the objects are available when they

50、’re needed. And more important, in C++ you must make sure that you destroy the objects when you’re done with them. </p><p>  That brings up an interesting question. If Java leaves the objects lying around, w

51、hat keeps them from filling up memory and halting your program? This is exactly the kind of problem that would occur in C++. This is where a bit of magic happens. Java has a garbage collector, which looks at all the obje

52、cts that were created with new and figures out which ones are not being referenced anymore. Then it releases the memory for those objects, so the memory can be used for new objects. This means that </p><p> 

53、 From: Wang Rui book. C++ database system development Detailed Explanation [M].Beijing: Electronics Industry Publishing House. 2002.</p><p><b>  中文譯文</b></p><p><b>  一切皆是對象<

54、/b></p><p>  “盡管以C++為基礎(chǔ),但Java是一種更純粹的面向?qū)ο蟪绦蛟O(shè)計語言”。</p><p>  無論C++還是Java都屬于雜合語言。但在Java中,設(shè)計者覺得這種雜合并不像在C++里那么重要。雜合語言允許采用多種編程風(fēng)格;之所以說C++是一種雜合語言,是因為它支持與C語言的向后兼容能力。由于C++是C的一個超集,所以包含的許多特性都是后者不具備的,這些特性使

55、C++在某些地方顯得過于復(fù)雜。</p><p>  Java語言首先便假定了我們只希望進行面向?qū)ο蟮某绦蛟O(shè)計。也就是說,正式用它設(shè)計之前,必須先將自己的思想轉(zhuǎn)入一個面向?qū)ο蟮氖澜纾ǔ窃缫蚜?xí)慣了這個世界的思維方式)。只有做好這個準(zhǔn)備工作,與其他OOP語言相比,才能體會到Java的易學(xué)易用。下面,我們將探討Java程序的基本組件,并體會為什么說Java乃至Java程序內(nèi)的一切都是對象。</p><

56、;p>  (1)用句柄操縱對象。</p><p>  每種編程語言都有自己的數(shù)據(jù)處理方式。有些時候,程序員必須時刻留意準(zhǔn)備處理的是什么類型。您曾利用一些特殊語法直接操作過對象,或處理過一些間接表示的對象嗎(C或C++里的指針)?所有這些在Java里都得到了簡化,任何東西都可看作對象。因此,我們可采用一種統(tǒng)一的語法,任何地方均可照搬不誤。但要注意,盡管將一切都“看作”對象,但操縱的標(biāo)識符實際是指向一個對象的“

57、句柄”(Handle)。在其他Java參考書里,還可看到有的人將其稱作一個“引用”,甚至一個“指針”??蓪⑦@一情形想象成用遙控板(句柄)操縱電視機(對象)。只要握住這個遙控板,就相當(dāng)于掌握了與電視機連接的通道。但一旦需要“換頻道”或者“關(guān)小聲音”,我們實際操縱的是遙控板(句柄),再由遙控板自己操縱電視機(對象)。如果要在房間里四處走走,并想保持對電視機的控制,那么手上拿著的是遙控板,而非電視機。此外,即使沒有電視機,遙控板亦可獨立存在。

58、也就是說,只是由于擁有一個句柄,并不表示必須有一個對象同它連接。所以如果想容納一個詞或句子,可創(chuàng)建一個String句柄:</p><p><b>  String s;</b></p><p>  但這里創(chuàng)建的只是句柄,并不是對象。若此時向s發(fā)送一條消息,就會獲得一個錯誤(運行期)。這是由于s實際并未與任何東西連接(即“沒有電視機”)。因此,一種更安全的做法是:創(chuàng)建一

59、個句柄時,記住無論如何都進行初始化:</p><p>  String s = "asdf";</p><p>  然而,這里采用的是一種特殊類型:字串可用加引號的文字初始化。通常,必須為對象使用一種更通用的初始化類型。</p><p>  (2)所有對象都必須創(chuàng)建。</p><p>  創(chuàng)建句柄時,我們希望它同一個新對象

60、連接。通常用new關(guān)鍵字達到這一目的。new的意思是:“把我變成這些對象的一種新類型”。所以在上面的例子中,可以說:</p><p>  String s = new String("asdf");</p><p>  它不僅指出“將我變成一個新字串”,也通過提供一個初始字串,指出了“如何生成這個新字串”。當(dāng)然,字串(String)并非唯一的類型。Java配套提供了數(shù)量

61、眾多的現(xiàn)成類型。對我們來講,最重要的就是記住能自行創(chuàng)建類型。事實上,這應(yīng)是Java程序設(shè)計的一項基本操作,是繼續(xù)本書后余部分學(xué)習(xí)的基礎(chǔ)。</p><p>  程序運行時,我們最好對數(shù)據(jù)保存到什么地方做到心中有數(shù)。特別要注意的是內(nèi)存的分配。有六個地方都可以保存數(shù)據(jù):</p><p>  寄存器。這是最快的保存區(qū)域,因為它位于和其他所有保存方式不同的地方:處理器內(nèi)部。然而,寄存器的數(shù)量十分有限

62、,所以寄存器是根據(jù)需要由編譯器分配。我們對此沒有直接的控制權(quán),也不可能在自己的程序里找到寄存器存在的任何蹤跡。</p><p>  堆棧。駐留于常規(guī)RAM(隨機訪問存儲器)區(qū)域,但可通過它的“堆棧指針”獲得處理的直接支持。堆棧指針若向下移,會創(chuàng)建新的內(nèi)存;若向上移,則會釋放那些內(nèi)存。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存器。創(chuàng)建程序時,Java編譯器必須準(zhǔn)確地知道堆棧內(nèi)保存的所有數(shù)據(jù)的“長度”以及“存

63、在時間”。這是由于它必須生成相應(yīng)的代碼,以便向上和向下移動指針。這一限制無疑影響了程序的靈活性,所以盡管有些Java數(shù)據(jù)要保存在堆棧里——特別是對象句柄,但Java對象并不放到其中。</p><p>  堆。一種常規(guī)用途的內(nèi)存池(也在RAM區(qū)域),其中保存了Java對象。和堆棧不同,“內(nèi)存堆”或“堆”(Heap)最吸引人的地方在于編譯器不必知道要從堆里分配多少存儲空間,也不必知道存儲的數(shù)據(jù)要在堆里停留多長的時間。

64、因此,用堆保存數(shù)據(jù)時會得到更大的靈活性。要求創(chuàng)建一個對象時,只需用new命令編制相關(guān)的代碼即可。執(zhí)行這些代碼時,會在堆里自動進行數(shù)據(jù)的保存。當(dāng)然,為達到這種靈活性,必然會付出一定的代價:在堆里分配存儲空間時會花掉更長的時間!</p><p>  靜態(tài)存儲。這兒的“靜態(tài)”(Static)是指“位于固定位置”(盡管也在RAM里)。程序運行期間,靜態(tài)存儲的數(shù)據(jù)將隨時等候調(diào)用??捎胹tatic關(guān)鍵字指出一個對象的特定元素

65、是靜態(tài)的。但Java對象本身永遠(yuǎn)都不會置入靜態(tài)存儲空間。</p><p>  常數(shù)存儲。常數(shù)值通常直接置于程序代碼內(nèi)部。這樣做是安全的,因為它們永遠(yuǎn)都不會改變。有的常數(shù)需要嚴(yán)格地保護,所以可考慮將它們置入只讀存儲器(ROM)。</p><p>  非RAM存儲。若數(shù)據(jù)完全獨立于一個程序之外,則程序不運行時仍可存在,并在程序的控制范圍之外。其中兩個最主要的例子便是“流式對象”和“固定對象”。

66、對于流式對象,對象會變成字節(jié)流,通常會發(fā)給另一臺機器。而對于固定對象,對象保存在磁盤中。即使程序中止運行,它們?nèi)钥杀3肿约旱臓顟B(tài)不變。對于這些類型的數(shù)據(jù)存儲,一個特別有用的技巧就是它們能存在于其他媒體中。一旦需要,甚至能將它們恢復(fù)成普通的、基于RAM的對象。Java 1.1提供了對Lightweight persistence的支持。未來的版本甚至可能提供更完整的方案。</p><p><b>  (3

67、)高精度數(shù)字</b></p><p>  Java 1.1增加了兩個類,用于進行高精度的計算:Big Integer和Big Decimal。盡管它們大致可以劃分為“封裝器”類型,但兩者都沒有對應(yīng)的“主類型”。</p><p>  這兩個類都有自己特殊的“方法”,對應(yīng)于我們針對主類型執(zhí)行的操作。也就是說,能對int或float做的事情,對Big Integer和Big Deci

68、mal一樣可以做。只是必須使用方法調(diào)用,不能使用運算符。此外,由于牽涉更多,所以運算速度會慢一些。我們犧牲了速度,但換來了精度。</p><p>  Big Integer支持任意精度的整數(shù)。也就是說,我們可精確表示任意大小的整數(shù)值,同時在運算過程中不會丟失任何信息。Big Decimal支持任意精度的定點數(shù)字。例如,可用它進行精確的幣值計算。至于調(diào)用這兩個類時可選用的構(gòu)建器和方法,請自行參考聯(lián)機幫助文檔。<

69、;/p><p>  (4)Java的數(shù)組</p><p>  幾乎所有程序設(shè)計語言都支持?jǐn)?shù)組。在C和C++里使用數(shù)組是非常危險的,因為那些數(shù)組只是內(nèi)存塊。若程序訪問自己內(nèi)存塊以外的數(shù)組,或者在初始化之前使用內(nèi)存(屬于常規(guī)編程錯誤),會產(chǎn)生不可預(yù)測的后果。</p><p>  Java的一項主要設(shè)計目標(biāo)就是安全性。所以在C和C++里困擾程序員的許多問題都未在Java里重復(fù)

70、。一個Java可以保證被初始化,而且不可在它的范圍之外訪問。由于系統(tǒng)自動進行范圍檢查,所以必然要付出一些代價:針對每個數(shù)組,以及在運行期間對索引的校驗,都會造成少量的內(nèi)存開銷。但由此換回的是更高的安全性,以及更高的工作效率。為此付出少許代價是值得的。</p><p>  創(chuàng)建對象數(shù)組時,實際創(chuàng)建的是一個句柄數(shù)組。而且每個句柄都會自動初始化成一個特殊值,并帶有自己的關(guān)鍵字:null(空)。一旦Java看到null,

71、就知道該句柄并未指向一個對象。正式使用前,必須為每個句柄都分配一個對象。若試圖使用依然為null的一個句柄,就會在運行期報告問題。因此,典型的數(shù)組錯誤在Java里就得到了避免。</p><p>  也可以創(chuàng)建主類型數(shù)組。同樣地,編譯器能夠擔(dān)保對它的初始化,因為會將那個數(shù)組的內(nèi)存劃分成零。</p><p>  (5)絕對不要清除對象</p><p>  在大多數(shù)程序設(shè)

72、計語言中,變量的“存在時間”(Lifetime)一直是程序員需要著重考慮的題。變量應(yīng)持續(xù)多長的時間?如果想清除它,那么何時進行?在變量存在時間上糾纏不清造成大量的程序錯誤。在下面的小節(jié)里,將闡釋Java如何幫助我們完成所有清除工作,從而極大了簡化了這個問題。</p><p><b>  作用域</b></p><p>  大多數(shù)程序設(shè)計語言都提供了“作用域”(Scop

73、e)的概念。對于在作用域里定義的名字,作用域同時決定了它的“可見性”以及“存在時間”。在C,C++和Java里,作用域是由花括號的位置決定的。</p><p>  作為在作用域里定義的一個變量,它只有在那個作用域結(jié)束之前才可使用。</p><p>  在上面的例子中,縮進排版使Java代碼更易辨讀。由于Java是一種形式自由的語言,所以額外的空格、制表位以及回車都不會對結(jié)果程序造成影響。編

74、譯器會認(rèn)為變量x已被定義。所以C和C++能將一個變量“隱藏”在一個更大的作用域里。但這種做法在Java里是不允許的。</p><p><b>  對象的作用域</b></p><p>  Java對象不具備與主類型一樣的存在時間。用new關(guān)鍵字創(chuàng)建一個Java對象的時候,它會超出作用域的范圍之外。</p><p>  那么句柄s會在作用域的終點

75、處消失。然而,s指向的String對象依然占據(jù)著內(nèi)存空間。在上面這段代碼里,我們沒有辦法訪問對象,因為指向它的唯一一個句柄已超出了作用域的邊界。在后面的章節(jié)里,大家還會繼續(xù)學(xué)習(xí)如何在程序運行期間傳遞和復(fù)制對象句柄。</p><p>  這樣造成的結(jié)果便是:對于用new創(chuàng)建的對象,只要我們愿意,它們就會一直保留下去。這個編程問題在C和C++里特別突出??磥碓贑++里遇到的麻煩最大:由于不能從語言獲得任何幫助,所以在

76、需要對象的時候,根本無法確定它們是否可用。而且更麻煩的是,在C++里,一旦工作完成,必須保證將對象清除。</p><p>  這樣便帶來了一個有趣的問題。假如Java讓對象依然故我,怎樣才能防止它們大量充斥內(nèi)存,并最終造成程序的“凝固”呢。在C++里,這個問題最令程序員頭痛。但Java以后,情況卻發(fā)生了改觀。Java有一個特別的“垃圾收集器”,它會查找用new創(chuàng)建的所有對象,并辨別其中哪些不再被引用。隨后,它會自

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論