計算機科學與技術外文資料翻譯_第1頁
已閱讀1頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、<p>  畢業(yè)設計(論文)外文資料翻譯</p><p>  系: 計算機系 </p><p>  專 業(yè): 計算機科學與技術 </p><p>  姓 名: </p&

2、gt;<p>  學 號: 080601542 </p><p>  外文出處:http://www.javaworld.com/javaworld/</p><p>  jw-09-2000/jw-0908-eckelobjects.html </p

3、><p>  附 件: 1.外文資料翻譯譯文;2.外文原文。 </p><p>  注:請將該封面與附件裝訂成冊。</p><p>  附件1:外文資料翻譯譯文</p><p><b>  一切都是對象</b></p><p>  “盡管以C++為基礎,但Java是一種更純粹的面向?qū)ο蟪绦蛟O計語

4、言”。</p><p>  無論C++還是Java都屬于雜合語言。但在Java中,設計者覺得這種雜合并不象在C++里那么重要。雜合語言允許采用多種編程風格;之所以說C++是一種雜合語言,是因為它支持與C語言的向后兼容能力。由于C++是C的一個超集,所以包含的許多特性都是后者不具備的,這些特性使C++在某些地方顯得過于復雜。</p><p>  Java語言首先便假定了我們只希望進行面向?qū)ο?/p>

5、的程序設計。也就是說,正式用它設計之前,必須先將自己的思想轉(zhuǎn)入一個面向?qū)ο蟮氖澜纾ǔ窃缫蚜晳T了這個世界的思維方式)。只有做好這個準備工作,與其他OOP語言相比,才能體會到Java的易學易用。在本章,我們將探討Java程序的基本組件,并體會為什么說Java乃至Java程序內(nèi)的一切都是對象。</p><p>  1. 用句柄操縱對象</p><p>  每種編程語言都有自己的數(shù)據(jù)處理方式。有

6、些時候,程序員必須時刻留意準備處理的是什么類型。您曾利用一些特殊語法直接操作過對象,或處理過一些間接表示的對象嗎(C或C++里的指針)?</p><p>  所有這些在Java里都得到了簡化,任何東西都可看作對象。因此,我們可采用一種統(tǒng)一的語法,任何地方均可照搬不誤。但要注意,盡管將一切都“看作”對象,但操縱的標識符實際是指向一個對象的“句柄”(Handle)。在其他Java參考書里,還可看到有的人將其稱作一個“

7、引用”,甚至一個“指針”。可將這一情形想象成用遙控板(句柄)操縱電視機(對象)。只要握住這個遙控板,就相當于掌握了與電視機連接的通道。但一旦需要“換頻道”或者“關小聲音”,我們實際操縱的是遙控板(句柄),再由遙控板自己操縱電視機(對象)。如果要在房間里四處走走,并想保持對電視機的控制,那么手上拿著的是遙控板,而非電視機。</p><p>  此外,即使沒有電視機,遙控板亦可獨立存在。也就是說,只是由于擁有一個句柄

8、,并不表示必須有一個對象同它連接。所以如果想容納一個詞或句子,可創(chuàng)建一個String句柄:String s;但這里創(chuàng)建的只是句柄,并不是對象。若此時向s發(fā)送一條消息,就會獲得一個錯誤(運行期)。這是由于s實際并未與任何東西連接(即“沒有電視機”)。因此,一種更安全的做法是:創(chuàng)建一個句柄時,記住無論如何都進行初始化:String s = "asdf";然而,這里采用的是一種特殊類型:字串可用加引號的文字初始化。通常,必

9、須為對象使用一種更通用的初始化類型。</p><p>  2. 所有對象都必須創(chuàng)建</p><p>  創(chuàng)建句柄時,我們希望它同一個新對象連接。通常用new關鍵字達到這一目的。new的意思是:“把我變成這些對象的一種新類型”。所以在上面的例子中,可以說:String s = new String("asdf");它不僅指出“將我變成一個新字串”,也通過提供一個初始字串,

10、指出了“如何生成這個新字串”。當然,字串(String)并非唯一的類型。Java配套提供了數(shù)量眾多的現(xiàn)成類型。對我們來講,最重要的就是記住能自行創(chuàng)建類型。事實上,這應是Java程序設計的一項基本操作,是繼續(xù)本書后余部分學習的基礎。</p><p>  3. 保存到什么地方</p><p>  程序運行時,我們最好對數(shù)據(jù)保存到什么地方做到心中有數(shù)。特別要注意的是內(nèi)存的分配。有六個地方都可以保

11、存數(shù)據(jù):</p><p>  (1) 寄存器。這是最快的保存區(qū)域,因為它位于和其他所有保存方式不同的地方:處理器內(nèi)部。然而,寄存器的數(shù)量十分有限,所以寄存器是根據(jù)需要由編譯器分配。我們對此沒有直接的控制權,也不可能在自己的程序里找到寄存器存在的任何蹤跡。</p><p>  (2) 堆棧。駐留于常規(guī)RAM(隨機訪問存儲器)區(qū)域,但可通過它的“堆棧指針”獲得處理的直接支持。堆棧指針若向下移,

12、會創(chuàng)建新的內(nèi)存;若向上移,則會釋放那些內(nèi)存。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存器。創(chuàng)建程序時,Java編譯器必須準確地知道堆棧內(nèi)保存的所有數(shù)據(jù)的“長度”以及“存在時間”。這是由于它必須生成相應的代碼,以便向上和向下移動指針。這一限制無疑影響了程序的靈活性,所以盡管有些Java數(shù)據(jù)要保存在堆棧里——特別是對象句柄,但Java對象并不放到其中。</p><p>  (3) 堆。一種常規(guī)用途的內(nèi)存池(也

13、在RAM區(qū)域),其中保存了Java對象。和堆棧不同,“內(nèi)存堆”或“堆”(Heap)最吸引人的地方在于編譯器不必知道要從堆里分配多少存儲空間,也不必知道存儲的數(shù)據(jù)要在堆里停留多可。執(zhí)行這些代碼時,會在堆里自動進行數(shù)據(jù)的保存。當然,為達到這種靈活性,必然會付出一定的代價:在堆里分配存儲空間時會花掉更長的時間!</p><p>  (4) 靜態(tài)存儲。這兒的“靜態(tài)”(Static)是指“位于固定位置”(盡管也在RAM里)

14、。程序運行期間,靜態(tài)存儲的數(shù)據(jù)將隨時等候調(diào)用??捎胹tatic關鍵字指出一個對象的特定元素是靜態(tài)的。但Java對象本身永遠都不會置入靜態(tài)存儲空間。1703java對象英文文獻及翻譯</p><p>  (5) 常數(shù)存儲。常數(shù)值通常直接置于程序代碼內(nèi)部。這樣做是安全的,因為它們永遠都不會改變。有的常數(shù)需要嚴格地保護,所以可考慮將它們置入只讀存儲器(ROM)。</p><p>  (6) 非R

15、AM存儲。若數(shù)據(jù)完全獨立于一個程序之外,則程序不運行時仍可存在,并在程序的控制范圍之外。其中兩個最主要的例子便是“流式對象”和“固定對象”。對于流式對象,對象會變成字節(jié)流,通常會發(fā)給另一臺機器。而對于固定對象,對象保存在磁盤中。即使程序中止運行,它們?nèi)钥杀3肿约旱臓顟B(tài)不變。對于這些類型的數(shù)據(jù)存儲,一個特別有用的技巧就是它們能存在于其他媒體中。一旦需要,甚至能將它們恢復成普通的、基于RAM的對象。Java 1.1提供了對Lightweig

16、ht persistence的支持。未來的版本甚至可能提供更完整的方案。</p><p>  4. 特殊情況:主要類型</p><p>  有一系列類需特別對待;可將它們想象成“基本”、“主要”或者“主”(Primitive)類型,進行程序設計時要頻繁用到它們。之所以要特別對待,是由于用new創(chuàng)建對象(特別是小的、簡單的變量)并不是非常有效,因為new將對象置于“堆”里。對于這些類型,Ja

17、va采納了與C和C++相同的方法。也就是說,不是用new創(chuàng)建變量,而是創(chuàng)建一個并非句柄的“自動”變量。飧霰淞咳菽閃司嚀宓鬧擔⒅糜詼顏恢?,能够更高效地磱堋?BR>Java決定了每種主要類型的大小。就象在大多數(shù)語言里那樣,這些大小并不隨著機器結構的變化而變化。這種大小的不可更改正是Java程序具有很強移植能力的原因之一。</p><p>  數(shù)值類型全都是有符號(正負號)的,所以不必費勁尋找沒有符號的類型。

18、</p><p>  主數(shù)據(jù)類型也擁有自己的“封裝器”(wrapper)類。這意味著假如想讓堆內(nèi)一個非主要對象表示那個主類型,就要使用對應的封裝器。例如:char c = 'x';</p><p>  Character C = new Character('c');</p><p>  也可以直接使用:Character C = n

19、ew Character('x');</p><p><b>  5. 高精度數(shù)字</b></p><p>  Java 1.1增加了兩個類,用于進行高精度的計算:BigInteger和BigDecimal。盡管它們大致可以劃分為“封裝器”類型,但兩者都沒有對應的“主類型”。</p><p>  這兩個類都有自己特殊的“方法”,

20、對應于我們針對主類型執(zhí)行的操作。也就是說,能對int或float做的事情,對BigInteger和BigDecimal一樣可以做。只是必須使用方法調(diào)用,不能使用運算符。此外,由于牽涉更多,所以運算速度會慢一些。我們犧牲了速度,但換來了精度。</p><p>  BigInteger支持任意精度的整數(shù)。也就是說,我們可精確表示任意大小的整數(shù)值,同時在運算過程中不會丟失任何信息。</p><p&g

21、t;  BigDecimal支持任意精度的定點數(shù)字。例如,可用它進行精確的幣值計算。</p><p>  至于調(diào)用這兩個類時可選用的構建器和方法,請自行參考聯(lián)機幫助文檔。</p><p>  6. Java的數(shù)組</p><p>  幾乎所有程序設計語言都支持數(shù)組。在C和C++里使用數(shù)組是非常危險的,因為那些數(shù)組只是內(nèi)存塊。若程序訪問自己內(nèi)存塊以外的數(shù)組,或者在初始

22、化之前使用內(nèi)存(屬于常規(guī)編程錯誤),會產(chǎn)生不可預測的后果。在C++里,應盡量不要使用數(shù)組,換用標準模板庫(Standard TemplateLibrary)里更安全的容器。Java的一項主要設計目標就是安全性。所以在C和C++里困擾程序員的許多問題都未在Java里重復。一個Java可以保證被初始化,而且不可在它的范圍之外訪問。由于系統(tǒng)自動進行范圍檢查,所以必然要付出一些代價:針對每個數(shù)組,以及在運行期間對索引的校驗,都會造成少量的內(nèi)存開

23、銷。但由此換回的是更高的安全性,以及更高的工作效率。為此付出少許代價是值得的。</p><p>  創(chuàng)建對象數(shù)組時,實際創(chuàng)建的是一個句柄數(shù)組。而且每個句柄都會自動初始化成一個特殊值,并帶有自己的關鍵字:null(空)。一旦Java看到null,就知道該句柄并未指向一個對象。正式使用前,必須為每個句柄都分配一個對象。若試圖使用依然為null的一個句柄,就會在運行期報告問題。因此,典型的數(shù)組錯誤在Java里就得到了避

24、免。</p><p>  也可以創(chuàng)建主類型數(shù)組。同樣地,編譯器能夠擔保對它的初始化,因為會將那個數(shù)組的內(nèi)存劃分成零。</p><p>  數(shù)組問題將在以后的章節(jié)里詳細討論。</p><p>  7. 絕對不要清除對象</p><p>  在大多數(shù)程 如何幫助我們完成所有清除工作,從而極大了簡化了這個問題。</p><p&g

25、t;<b>  8. 作用域</b></p><p>  大多數(shù)程序設計語言都提供了“作用域”(Scope)的概念。對于在作用域里定義的名字,作用域同時決定了它的“可見性”以及“存在時間”。在C,C++和Java里,作用域是由花括號的位置決定的。參考下面這個例子:</p><p><b>  {</b></p><p> 

26、 int x = 12;</p><p><b>  {</b></p><p>  int q = 96; </p><p><b>  } </b></p><p><b>  }</b></p><p>  作為在作用域里定義的一個變量,它只

27、有在那個作用域結束之前才可使用。</p><p>  在上面的例子中,縮進排版使Java代碼更易辨讀。由于Java是一種形式自由的語言,所以額外的空格、制表位以及回車都不會對結果程序造成影響。</p><p>  注意盡管在C和C++里是合法的,但在Java里不能象下面這樣書寫代碼:</p><p><b>  {</b></p>

28、<p>  int x = 12;</p><p><b>  {</b></p><p>  int x = 96; </p><p><b>  }</b></p><p><b>  }</b></p><p>  編譯器會認為變量x已

29、被定義。所以C和C++能將一個變量“隱藏”在一個更大的作用域里。但這種做法在Java里是不允許的,因為Java的設計者認為這樣做使程序產(chǎn)生了混淆。</p><p><b>  9. 對象的作用域</b></p><p>  Java對象不具備與主類型一樣的存在時間。用new關鍵字創(chuàng)建一個Java對象的時候,它會超出作用域的范圍之外。所以假若使用下面這段代碼: <

30、;/p><p>  附件2:外文原文(復印件)</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 Jav

31、a 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 c

32、ompatibility 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 Ja

33、va 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 in

34、itial 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 components of a Java program and we’ll learn that everything in

35、Java is 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

36、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 spe

37、cial syntax?</p><p>  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 actuall

38、y 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 object connected to it. So if you wa

39、nt to hold a word or sentence, you create a String reference: String s;But here you’ve created </p><p>  You must create all the objects</p><p>  When you create a reference, you want to connect

40、 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: String s = new String("asdf");Not only d

41、oes this mean “Make me a new String,” but it also gives information about how to make the String by supplying an initial character string.</p><p>  Of course, String is not the only type that exists. Java co

42、mes 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 it’s what you’ll be learning about in the rest of th

43、is 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. There are six different places to store data:</p>

44、<p>  1. 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 registers is severely limited, so registers are allocat

45、ed 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. 2. The stack. This lives in the general random-access memory (RAM) area, b

46、ut has direct support from the processor via its stack pointer. The sta</p><p>  3. The heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. The nice thing about

47、 the heap is 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 usin

48、g storage on 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 hea</p><p>  4. Static storage. “Static” is used here in

49、 the sense of “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 elemen

50、t of an object is static, but Java objects themselves are never placed in static storage.</p><p>  5. Constant storage. Constant values are often placed directly in the program code, which is safe since they

51、 can never change. Sometimes constants are cordoned off by themselves so that they can be optionally placed in read-only memory (ROM), in embedded systems.</p><p>  6. Non-RAM storage. If data lives complete

52、ly outside a program, 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

53、 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 obje

54、cts into something</p><p>  Special 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 “primit

55、ive” 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

56、 that is not a reference. The variable holds the value, and it’s placed on the stack, so it’s much more efficient.</p><p>  Java determines the size of each primitive type. These sizes don’t change from one

57、machine architecture to another as they do in most languages. This size invariance is one reason Java programs are portable.</p><p>  All numeric types are signed, so don’t look for unsigned types.</p>

58、<p>  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.</p><p>  The “wrapper” classes for the primitive data types allo

59、w you to make a nonprimitive object on the heap to represent that primitive type. For example:</p><p>  char c = 'x';</p><p>  Character C = new Character(c);Or you could also use:</p

60、><p>  Character C = new Character('x');</p><p>  The reasons for doing this will be shown in a later chapter.</p><p>  High-precision numbers</p><p>  Java includ

61、es two classes for performing high-precision arithmetic: BigInteger and BigDecimal. Although these approximately fit into the same category as the “wrapper” classes, neither one has a primitive analogue.</p><p

62、>  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 or BigDecimal that you can with an int or float, it’s just tha

63、t you must use method calls instead of operators. Also, since there’s more involved, the operations will be slower. You’re exchanging speed for accuracy.</p><p>  BigInteger supports arbitrary-precision inte

64、gers. This means that 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 us

65、e these for accurate 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>  Arrays

66、 in Java</p><p>  Virtually all programming languages support arrays. Using arrays in C and C++ is perilous because those arrays are only blocks of memory. If a program accesses the array outside of its memo

67、ry block or uses the memory before initialization (common programming errors), there will be unpredictable results.</p><p>  One of the primary goals of Java is safety, so many of the problems that plague pr

68、ogrammers in C and C++ are not repeated in Java. A Java array is guaranteed to be initialized and cannot be accessed outside of its range. The range checking comes at the price of having a small amount of memory overhead

69、 on each array as well as verifying the index at run time, but the assumption is that the safety and increased productivity is worth the expense.</p><p>  When you create an array of objects, you are really

70、creating an array of references, and each of those references is automatically initialized to a special value with its own keyword: null. When Java sees null, it recognizes that the reference in question isn’t pointing t

71、o 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 null, the problem will be reported at run time. Thus, typical array errors are prevented in Java.

72、</p><p>  You can also create an array of primitives. Again, the compiler guarantees initialization because it zeroes the memory for that array.</p><p>  Arrays will be covered in detail in late

73、r chapters.You never need to</p><p>  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

74、. How long does the 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 al

75、l the cleanup work 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 define

76、d within that scope. In C, C++, and Java, scope is determined by the placement of curly braces {}. So for example:</p><p>  { int x = 12;</p><p>  // Only x available</p><p>  { int

77、 q = 96;</p><p>  // Both x & q available</p><p><b>  }</b></p><p>  // Only x available</p><p>  // q “out of scope”</p><p><b>  }&l

78、t;/b></p><p>  A variable defined within a scope is available only to the end of that scope.</p><p>  Any text after a ‘//’ to the end of a line is a comment.</p><p>  Indentatio

79、n 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 program.</p><p>  Note that you cannot do the following, even th

80、ough it is legal in C and C++:</p><p>  {int x = 12;</p><p>  {int x = 96; // Illegal</p><p><b>  }}</b></p><p>  The compiler will announce that the variab

81、le x has already been defined. Thus the C and C++ ability to “hide” a variable in a larger scope is not allowed, because the Java designers thought that it led to confusing programs.</p><p>  Scope of object

82、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. Thus if you use:</p><p><b>  {<

83、;/b></p><p>  String s = new String("a string");</p><p>  } // End of scope</p><p>  the reference s vanishes at the end of the scope. However, the String object that s

84、 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 chapters you’ll see how the reference to the object can be

85、 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 want them, a whole slew of C++ programming problems si

86、mply 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’re needed. And more important, in C++ you must make

溫馨提示

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

評論

0/150

提交評論