版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、<p> 畢業(yè)設(shè)計(jì)(論文)外文資料翻譯</p><p> 系: 計(jì)算機(jī)系 </p><p> 專 業(yè): 計(jì)算機(jī)科學(xué)與技術(shù) </p><p> 姓 名: </p&
2、gt;<p> 學(xué) 號(hào): 080601542 </p><p> 外文出處:http://www.javaworld.com/javaworld/</p><p> jw-09-2000/jw-0908-eckelobjects.html </p
3、><p> 附 件: 1.外文資料翻譯譯文;2.外文原文。 </p><p> 注:請(qǐng)將該封面與附件裝訂成冊(cè)。</p><p> 附件1:外文資料翻譯譯文</p><p><b> 一切都是對(duì)象</b></p><p> “盡管以C++為基礎(chǔ),但Java是一種更純粹的面向?qū)ο蟪绦蛟O(shè)計(jì)語
4、言”。</p><p> 無論C++還是Java都屬于雜合語言。但在Java中,設(shè)計(jì)者覺得這種雜合并不象在C++里那么重要。雜合語言允許采用多種編程風(fēng)格;之所以說C++是一種雜合語言,是因?yàn)樗С峙cC語言的向后兼容能力。由于C++是C的一個(gè)超集,所以包含的許多特性都是后者不具備的,這些特性使C++在某些地方顯得過于復(fù)雜。</p><p> Java語言首先便假定了我們只希望進(jìn)行面向?qū)ο?/p>
5、的程序設(shè)計(jì)。也就是說,正式用它設(shè)計(jì)之前,必須先將自己的思想轉(zhuǎn)入一個(gè)面向?qū)ο蟮氖澜纾ǔ窃缫蚜?xí)慣了這個(gè)世界的思維方式)。只有做好這個(gè)準(zhǔn)備工作,與其他OOP語言相比,才能體會(huì)到Java的易學(xué)易用。在本章,我們將探討Java程序的基本組件,并體會(huì)為什么說Java乃至Java程序內(nèi)的一切都是對(duì)象。</p><p> 1. 用句柄操縱對(duì)象</p><p> 每種編程語言都有自己的數(shù)據(jù)處理方式。有
6、些時(shí)候,程序員必須時(shí)刻留意準(zhǔn)備處理的是什么類型。您曾利用一些特殊語法直接操作過對(duì)象,或處理過一些間接表示的對(duì)象嗎(C或C++里的指針)?</p><p> 所有這些在Java里都得到了簡化,任何東西都可看作對(duì)象。因此,我們可采用一種統(tǒng)一的語法,任何地方均可照搬不誤。但要注意,盡管將一切都“看作”對(duì)象,但操縱的標(biāo)識(shí)符實(shí)際是指向一個(gè)對(duì)象的“句柄”(Handle)。在其他Java參考書里,還可看到有的人將其稱作一個(gè)“
7、引用”,甚至一個(gè)“指針”??蓪⑦@一情形想象成用遙控板(句柄)操縱電視機(jī)(對(duì)象)。只要握住這個(gè)遙控板,就相當(dāng)于掌握了與電視機(jī)連接的通道。但一旦需要“換頻道”或者“關(guān)小聲音”,我們實(shí)際操縱的是遙控板(句柄),再由遙控板自己操縱電視機(jī)(對(duì)象)。如果要在房間里四處走走,并想保持對(duì)電視機(jī)的控制,那么手上拿著的是遙控板,而非電視機(jī)。</p><p> 此外,即使沒有電視機(jī),遙控板亦可獨(dú)立存在。也就是說,只是由于擁有一個(gè)句柄
8、,并不表示必須有一個(gè)對(duì)象同它連接。所以如果想容納一個(gè)詞或句子,可創(chuàng)建一個(gè)String句柄:String s;但這里創(chuàng)建的只是句柄,并不是對(duì)象。若此時(shí)向s發(fā)送一條消息,就會(huì)獲得一個(gè)錯(cuò)誤(運(yùn)行期)。這是由于s實(shí)際并未與任何東西連接(即“沒有電視機(jī)”)。因此,一種更安全的做法是:創(chuàng)建一個(gè)句柄時(shí),記住無論如何都進(jìn)行初始化:String s = "asdf";然而,這里采用的是一種特殊類型:字串可用加引號(hào)的文字初始化。通常,必
9、須為對(duì)象使用一種更通用的初始化類型。</p><p> 2. 所有對(duì)象都必須創(chuàng)建</p><p> 創(chuàng)建句柄時(shí),我們希望它同一個(gè)新對(duì)象連接。通常用new關(guān)鍵字達(dá)到這一目的。new的意思是:“把我變成這些對(duì)象的一種新類型”。所以在上面的例子中,可以說:String s = new String("asdf");它不僅指出“將我變成一個(gè)新字串”,也通過提供一個(gè)初始字串,
10、指出了“如何生成這個(gè)新字串”。當(dāng)然,字串(String)并非唯一的類型。Java配套提供了數(shù)量眾多的現(xiàn)成類型。對(duì)我們來講,最重要的就是記住能自行創(chuàng)建類型。事實(shí)上,這應(yīng)是Java程序設(shè)計(jì)的一項(xiàng)基本操作,是繼續(xù)本書后余部分學(xué)習(xí)的基礎(chǔ)。</p><p> 3. 保存到什么地方</p><p> 程序運(yùn)行時(shí),我們最好對(duì)數(shù)據(jù)保存到什么地方做到心中有數(shù)。特別要注意的是內(nèi)存的分配。有六個(gè)地方都可以保
11、存數(shù)據(jù):</p><p> (1) 寄存器。這是最快的保存區(qū)域,因?yàn)樗挥诤推渌斜4娣绞讲煌牡胤剑禾幚砥鲀?nèi)部。然而,寄存器的數(shù)量十分有限,所以寄存器是根據(jù)需要由編譯器分配。我們對(duì)此沒有直接的控制權(quán),也不可能在自己的程序里找到寄存器存在的任何蹤跡。</p><p> (2) 堆棧。駐留于常規(guī)RAM(隨機(jī)訪問存儲(chǔ)器)區(qū)域,但可通過它的“堆棧指針”獲得處理的直接支持。堆棧指針若向下移,
12、會(huì)創(chuàng)建新的內(nèi)存;若向上移,則會(huì)釋放那些內(nèi)存。這是一種特別快、特別有效的數(shù)據(jù)保存方式,僅次于寄存器。創(chuàng)建程序時(shí),Java編譯器必須準(zhǔn)確地知道堆棧內(nèi)保存的所有數(shù)據(jù)的“長度”以及“存在時(shí)間”。這是由于它必須生成相應(yīng)的代碼,以便向上和向下移動(dòng)指針。這一限制無疑影響了程序的靈活性,所以盡管有些Java數(shù)據(jù)要保存在堆棧里——特別是對(duì)象句柄,但Java對(duì)象并不放到其中。</p><p> (3) 堆。一種常規(guī)用途的內(nèi)存池(也
13、在RAM區(qū)域),其中保存了Java對(duì)象。和堆棧不同,“內(nèi)存堆”或“堆”(Heap)最吸引人的地方在于編譯器不必知道要從堆里分配多少存儲(chǔ)空間,也不必知道存儲(chǔ)的數(shù)據(jù)要在堆里停留多可。執(zhí)行這些代碼時(shí),會(huì)在堆里自動(dòng)進(jìn)行數(shù)據(jù)的保存。當(dāng)然,為達(dá)到這種靈活性,必然會(huì)付出一定的代價(jià):在堆里分配存儲(chǔ)空間時(shí)會(huì)花掉更長的時(shí)間!</p><p> (4) 靜態(tài)存儲(chǔ)。這兒的“靜態(tài)”(Static)是指“位于固定位置”(盡管也在RAM里)
14、。程序運(yùn)行期間,靜態(tài)存儲(chǔ)的數(shù)據(jù)將隨時(shí)等候調(diào)用??捎胹tatic關(guān)鍵字指出一個(gè)對(duì)象的特定元素是靜態(tài)的。但Java對(duì)象本身永遠(yuǎn)都不會(huì)置入靜態(tài)存儲(chǔ)空間。1703java對(duì)象英文文獻(xiàn)及翻譯</p><p> (5) 常數(shù)存儲(chǔ)。常數(shù)值通常直接置于程序代碼內(nèi)部。這樣做是安全的,因?yàn)樗鼈冇肋h(yuǎn)都不會(huì)改變。有的常數(shù)需要嚴(yán)格地保護(hù),所以可考慮將它們置入只讀存儲(chǔ)器(ROM)。</p><p> (6) 非R
15、AM存儲(chǔ)。若數(shù)據(jù)完全獨(dú)立于一個(gè)程序之外,則程序不運(yùn)行時(shí)仍可存在,并在程序的控制范圍之外。其中兩個(gè)最主要的例子便是“流式對(duì)象”和“固定對(duì)象”。對(duì)于流式對(duì)象,對(duì)象會(huì)變成字節(jié)流,通常會(huì)發(fā)給另一臺(tái)機(jī)器。而對(duì)于固定對(duì)象,對(duì)象保存在磁盤中。即使程序中止運(yùn)行,它們?nèi)钥杀3肿约旱臓顟B(tài)不變。對(duì)于這些類型的數(shù)據(jù)存儲(chǔ),一個(gè)特別有用的技巧就是它們能存在于其他媒體中。一旦需要,甚至能將它們恢復(fù)成普通的、基于RAM的對(duì)象。Java 1.1提供了對(duì)Lightweig
16、ht persistence的支持。未來的版本甚至可能提供更完整的方案。</p><p> 4. 特殊情況:主要類型</p><p> 有一系列類需特別對(duì)待;可將它們想象成“基本”、“主要”或者“主”(Primitive)類型,進(jìn)行程序設(shè)計(jì)時(shí)要頻繁用到它們。之所以要特別對(duì)待,是由于用new創(chuàng)建對(duì)象(特別是小的、簡單的變量)并不是非常有效,因?yàn)閚ew將對(duì)象置于“堆”里。對(duì)于這些類型,Ja
17、va采納了與C和C++相同的方法。也就是說,不是用new創(chuàng)建變量,而是創(chuàng)建一個(gè)并非句柄的“自動(dòng)”變量。飧霰淞咳菽閃司嚀宓鬧擔(dān)⒅糜詼顏恢?,能够更高效地磱堋?BR>Java決定了每種主要類型的大小。就象在大多數(shù)語言里那樣,這些大小并不隨著機(jī)器結(jié)構(gòu)的變化而變化。這種大小的不可更改正是Java程序具有很強(qiáng)移植能力的原因之一。</p><p> 數(shù)值類型全都是有符號(hào)(正負(fù)號(hào))的,所以不必費(fèi)勁尋找沒有符號(hào)的類型。
18、</p><p> 主數(shù)據(jù)類型也擁有自己的“封裝器”(wrapper)類。這意味著假如想讓堆內(nèi)一個(gè)非主要對(duì)象表示那個(gè)主類型,就要使用對(duì)應(yīng)的封裝器。例如: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增加了兩個(gè)類,用于進(jìn)行高精度的計(jì)算:BigInteger和BigDecimal。盡管它們大致可以劃分為“封裝器”類型,但兩者都沒有對(duì)應(yīng)的“主類型”。</p><p> 這兩個(gè)類都有自己特殊的“方法”,
20、對(duì)應(yīng)于我們針對(duì)主類型執(zhí)行的操作。也就是說,能對(duì)int或float做的事情,對(duì)BigInteger和BigDecimal一樣可以做。只是必須使用方法調(diào)用,不能使用運(yùn)算符。此外,由于牽涉更多,所以運(yùn)算速度會(huì)慢一些。我們犧牲了速度,但換來了精度。</p><p> BigInteger支持任意精度的整數(shù)。也就是說,我們可精確表示任意大小的整數(shù)值,同時(shí)在運(yùn)算過程中不會(huì)丟失任何信息。</p><p&g
21、t; BigDecimal支持任意精度的定點(diǎn)數(shù)字。例如,可用它進(jìn)行精確的幣值計(jì)算。</p><p> 至于調(diào)用這兩個(gè)類時(shí)可選用的構(gòu)建器和方法,請(qǐng)自行參考聯(lián)機(jī)幫助文檔。</p><p> 6. Java的數(shù)組</p><p> 幾乎所有程序設(shè)計(jì)語言都支持?jǐn)?shù)組。在C和C++里使用數(shù)組是非常危險(xiǎn)的,因?yàn)槟切?shù)組只是內(nèi)存塊。若程序訪問自己內(nèi)存塊以外的數(shù)組,或者在初始
22、化之前使用內(nèi)存(屬于常規(guī)編程錯(cuò)誤),會(huì)產(chǎn)生不可預(yù)測的后果。在C++里,應(yīng)盡量不要使用數(shù)組,換用標(biāo)準(zhǔn)模板庫(Standard TemplateLibrary)里更安全的容器。Java的一項(xiàng)主要設(shè)計(jì)目標(biāo)就是安全性。所以在C和C++里困擾程序員的許多問題都未在Java里重復(fù)。一個(gè)Java可以保證被初始化,而且不可在它的范圍之外訪問。由于系統(tǒng)自動(dòng)進(jìn)行范圍檢查,所以必然要付出一些代價(jià):針對(duì)每個(gè)數(shù)組,以及在運(yùn)行期間對(duì)索引的校驗(yàn),都會(huì)造成少量的內(nèi)存開
23、銷。但由此換回的是更高的安全性,以及更高的工作效率。為此付出少許代價(jià)是值得的。</p><p> 創(chuàng)建對(duì)象數(shù)組時(shí),實(shí)際創(chuàng)建的是一個(gè)句柄數(shù)組。而且每個(gè)句柄都會(huì)自動(dòng)初始化成一個(gè)特殊值,并帶有自己的關(guān)鍵字:null(空)。一旦Java看到null,就知道該句柄并未指向一個(gè)對(duì)象。正式使用前,必須為每個(gè)句柄都分配一個(gè)對(duì)象。若試圖使用依然為null的一個(gè)句柄,就會(huì)在運(yùn)行期報(bào)告問題。因此,典型的數(shù)組錯(cuò)誤在Java里就得到了避
24、免。</p><p> 也可以創(chuàng)建主類型數(shù)組。同樣地,編譯器能夠擔(dān)保對(duì)它的初始化,因?yàn)闀?huì)將那個(gè)數(shù)組的內(nèi)存劃分成零。</p><p> 數(shù)組問題將在以后的章節(jié)里詳細(xì)討論。</p><p> 7. 絕對(duì)不要清除對(duì)象</p><p> 在大多數(shù)程 如何幫助我們完成所有清除工作,從而極大了簡化了這個(gè)問題。</p><p&g
25、t;<b> 8. 作用域</b></p><p> 大多數(shù)程序設(shè)計(jì)語言都提供了“作用域”(Scope)的概念。對(duì)于在作用域里定義的名字,作用域同時(shí)決定了它的“可見性”以及“存在時(shí)間”。在C,C++和Java里,作用域是由花括號(hào)的位置決定的。參考下面這個(gè)例子:</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> 作為在作用域里定義的一個(gè)變量,它只
27、有在那個(gè)作用域結(jié)束之前才可使用。</p><p> 在上面的例子中,縮進(jìn)排版使Java代碼更易辨讀。由于Java是一種形式自由的語言,所以額外的空格、制表位以及回車都不會(huì)對(duì)結(jié)果程序造成影響。</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> 編譯器會(huì)認(rèn)為變量x已
29、被定義。所以C和C++能將一個(gè)變量“隱藏”在一個(gè)更大的作用域里。但這種做法在Java里是不允許的,因?yàn)镴ava的設(shè)計(jì)者認(rèn)為這樣做使程序產(chǎn)生了混淆。</p><p><b> 9. 對(duì)象的作用域</b></p><p> Java對(duì)象不具備與主類型一樣的存在時(shí)間。用new關(guān)鍵字創(chuàng)建一個(gè)Java對(duì)象的時(shí)候,它會(huì)超出作用域的范圍之外。所以假若使用下面這段代碼: <
30、;/p><p> 附件2:外文原文(復(fù)印件)</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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 計(jì)算機(jī)科學(xué)與技術(shù)外文翻譯
- 計(jì)算機(jī)科學(xué)與技術(shù)外文文獻(xiàn)翻譯
- 計(jì)算機(jī)科學(xué)外文翻譯
- 計(jì)算機(jī)外文資料翻譯
- 計(jì)算機(jī)專業(yè)外文資料翻譯
- 計(jì)算機(jī)科學(xué)與技術(shù)畢業(yè)設(shè)計(jì)(論文)外文翻譯
- 計(jì)算機(jī)科學(xué)與技術(shù)(計(jì)算機(jī)科學(xué)方向)專業(yè)
- 計(jì)算機(jī)科學(xué)與技術(shù)外文翻譯、中英對(duì)照、英漢互譯
- 計(jì)算機(jī)專業(yè)外文翻譯--計(jì)算機(jī)
- 計(jì)算機(jī)科學(xué)與技術(shù)
- 計(jì)算機(jī)外文翻譯---計(jì)算機(jī)引論
- 計(jì)算機(jī)外文資料翻譯---visual basic簡介
- 計(jì)算機(jī)外文資料翻譯---.net compact framework
- 計(jì)算機(jī)控制技術(shù)外文翻譯
- 計(jì)算機(jī)外文翻譯---java技術(shù)與ssh框架
- 計(jì)算機(jī)技術(shù)簡介外文翻譯
- 計(jì)算機(jī)外文翻譯
- 計(jì)算機(jī)專業(yè)外文翻譯---at89s52外文資料翻譯
- 計(jì)算機(jī)外文翻譯 ---網(wǎng)站建設(shè)技術(shù)
- 計(jì)算機(jī)科學(xué)與技術(shù)專業(yè)
評(píng)論
0/150
提交評(píng)論