【正文】
o), reducing a source of bugs. However, this initial value may not be correct or even legal for the program you are writing. It?s best to always explicitly initialize your variables. This guarantee doesn?t apply to local variables—those that are not fields of a class. Thus, if within a method definition you have: int x。 } void nothing() { return。 The type of the return value must be patible with the type of x. This act of calling a method is monly referred to as sending a message to an object. In the preceding example, the message is f( ) and the object is a. Objectoriented programming is often summarized as simply “sending messages to objects.” The argument list The method argument list specifies what information you pass into the method. As you might guess, this information—like everything else in Java—takes the form of objects. So, what you must specify in the argument list are the types of the objects to pass in and the name to use for each one. As in any situation in Java where you seem to be handing objects around, you are actually passing The type of the reference must be correct, however. If the argument is supposed to be a String, you must pass in a String or the piler will give an error. Consider a method that takes a String as its argument. Here is the definition, which must be placed within a class definition for it to be piled: int storage(String s) { return () * 2。 You can assign values to the fields, but you must first know how to refer to a member of an object. This is acplished by stating the name of the object reference, followed by a period (dot), followed by the name of the member inside the object: object Reference. Member or example: data. i = 47。 {int x = 96。 Or you could also use: Character ch = new Character(?x?)。至于本章,讀者只需要理解消息發(fā)送就足夠了。下面是一些例子: boolean flag(){return true。 } 此方法告訴你,需要多少個(gè)字節(jié) 才能容納一個(gè)特定的 String 對(duì)象中的信息( 字符串的每個(gè)字符的尺寸都是 16 位或 2 個(gè)字節(jié),以此來(lái)提供對(duì) Unicode 字符集的支持 ) 。面向?qū)ο蟮某绦蛟O(shè)計(jì)通常簡(jiǎn)單 地歸納為“向?qū)ο蟀l(fā)送消息”。如果試圖在某個(gè)對(duì)象上調(diào)用 它并不具備的方法,那么在編譯時(shí)就會(huì)得到一條錯(cuò)誤消息。盡管這只是用詞上的差別,但本書(shū)將沿用 Java 的慣用法,即用術(shù)語(yǔ)“方法”而不是“函數(shù)”來(lái)描述。因此如果在某個(gè)方法定義中有 int x。在這種情況下,只需要再使用連接句點(diǎn)即可。 double d。然而,你已經(jīng)可以用 new來(lái)創(chuàng)建這種類型的對(duì)象 。也就是說(shuō),你根本不必?fù)?dān)心內(nèi)存回收的問(wèn)題。 事實(shí) 證明,由 new 創(chuàng)建的對(duì)象,只要你需要,就會(huì)一直保留下去。因?yàn)?Java 設(shè)計(jì)者認(rèn)為這樣做會(huì)導(dǎo)致程序混亂。 q available } //only x available //q is “ out of scope” } 在作用域里定義的變量只可用于作用域結(jié)束之前。 永遠(yuǎn)不需要銷毀對(duì)象 在大多數(shù)程序設(shè)計(jì)語(yǔ)言中,變量生命周期的概念,占據(jù)了程序設(shè)計(jì)工作中非常重要的 部分 。但由此換來(lái)的是安全性和效率的提高,因此付出的代價(jià)是值得的(并且 Java 有時(shí)可以優(yōu)化這些操作)。 BigDecimal 支持任何精度的定點(diǎn)數(shù),例如,可以用它進(jìn)行精確的貨幣計(jì)算。雖然它們大體上屬于“包裝器類”的范疇,但二者都沒(méi)有對(duì)應(yīng)的基本類型。 所有數(shù)值類型都有正負(fù)號(hào),所以不要去尋找無(wú)符號(hào)的數(shù)值類型??梢园阉麄兿胂癯伞盎尽鳖愋汀F渲袃蓚€(gè)最重 要的例子便是“ 流式對(duì)象”和“持久化對(duì)象”。執(zhí)行這些代碼時(shí),會(huì)在堆里自動(dòng)進(jìn)行數(shù)據(jù)的保存。創(chuàng)建程序時(shí), Java 編譯器必須準(zhǔn)確地知道堆棧內(nèi)保存的所有數(shù)據(jù)的“長(zhǎng)度”以及“存在時(shí)間”。有六個(gè)地方都可以保存數(shù)據(jù): ( 1)寄存器。所以在上面的例子中,可以說(shuō): String s = new String(“ asdf” )。若此時(shí)向 s 發(fā)送一條消息,就會(huì)獲得一個(gè)錯(cuò)誤(運(yùn)行期)??蓪⑦@一情形想象成用遙控板(句柄)操縱電視機(jī)(對(duì)象)。只有做好這個(gè)準(zhǔn)備工作,與其他 OOP 語(yǔ)言相比,才能體會(huì)到 Java 的易學(xué)易用。 指導(dǎo)教師評(píng)語(yǔ): 簽名: 2021 年 月 日 (用外文寫(xiě) ) 附件 1:外文資料翻譯譯文 一切都是對(duì)象 “ 如 果我們說(shuō)另一種不同的語(yǔ)言,那么我們就會(huì)發(fā)覺(jué)一個(gè)有些不同的世界”。由于 C++是 C的一個(gè) 超集 ,所以包含的許多特性都是后者不具備的,這些特性使 C++在某些地方顯得過(guò)于復(fù)雜。因 此,我們可采用一種統(tǒng)一的語(yǔ)法,任何地方均可照搬不誤。也就是說(shuō),只是由于擁有一個(gè)句柄,并不表示必須有一個(gè)對(duì)象同它連接。 有對(duì)象都必須創(chuàng)建 創(chuàng)建句柄時(shí),我們希望它用一個(gè)新對(duì)象連接。 事實(shí)上,這應(yīng)是 Java 程序設(shè)計(jì)的一項(xiàng)基本操作,是繼續(xù)本書(shū)后余部分學(xué)習(xí)的基礎(chǔ)。駐留于常 規(guī) RAM(隨機(jī)訪問(wèn)存儲(chǔ)器)區(qū)域,但可通過(guò)它的“堆棧指針”獲得處理的直接支持。和堆棧不同,“內(nèi)存堆”或“堆”( Heap)最吸引人的地方在于編譯器不必知道要從堆里分配多少存儲(chǔ)空間,也不必知道存儲(chǔ)的數(shù)據(jù)要在堆里停留多長(zhǎng)時(shí)間。有的常數(shù)需要嚴(yán)格地保護(hù),所以可考慮將他們置入只讀存儲(chǔ)器( ROM) 。一旦需要,甚至能將他們恢復(fù)成普通的、基于 RAM 的對(duì)象。 Java 要確定每種基本類型所占存儲(chǔ)空間的大小。 也可以這樣用: Character ch = new Character(‘ x’ )。在這里,我們以速度換取了精度。 Java 的主要目標(biāo)之一是安全性,所以許多在 C和 C++里困擾程序員的問(wèn)題在 Java 里不會(huì)再出現(xiàn)呢。 還可以創(chuàng)建用來(lái)存放基本數(shù)據(jù)類型的數(shù)組。例如: { int x = 12。 { int x = 96。然而, s 指向的 String 對(duì)象仍繼續(xù)占據(jù)內(nèi)存空間。這也是 Java 神奇之 所在。 class 這個(gè)關(guān)鍵字(以后會(huì)頻繁使用,本書(shū)以后就不再用粗體字表示)之后緊跟著的是新類型的名稱。如果字段是對(duì)某個(gè)對(duì)象的引用,那么必須初始化該 引用,以便使其與一個(gè)實(shí)際的對(duì)象(如前所述,使用 new 來(lái)實(shí)現(xiàn))相關(guān)聯(lián)。 = 。但是,這些初始值對(duì)你的程序來(lái)說(shuō),可能是不正確的,甚至是不合法的。(許多 C++編譯器會(huì)對(duì)未初始化變 量給予警告,而 Java 則視為是錯(cuò)誤)。方法名和參數(shù)列表(它們合起來(lái)被稱為“方法簽名”)唯一地標(biāo)識(shí)出某個(gè)方法。 返回值的類型必須要與 x的類型兼容。如果參數(shù)被設(shè)為 String 類型,則必須傳遞一個(gè) String 對(duì)象;否則,編譯器將拋出錯(cuò)誤。其次,如果此方法產(chǎn)生一個(gè)值,這個(gè)值要放在 return 語(yǔ)句后面。但如果返回類型不是 void,那么無(wú)論在何處返回,編譯器都會(huì)強(qiáng)制返回一個(gè)正確類型的返回值。 C++, on the other hand, allow you to suggest register allocation to the piler). 2. The stack. This lives in the general randomaccess 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 memory. This is an extremely fast and efficient way to allocate storage, second only to 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 your programs, so while some Java storage exists on the stack—in particular, object references—Java objects themselves are not placed on the stack. Special case: primitive types 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 treatment 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, instead of creating the variable by using new, an “automatic” variable is created that is not a reference. The variable holds the value directly, and it?s placed on the stack, so it?s much more efficient. Java determines the size of each primitive type. These sizes don?t change from one machine 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 numeric types are signed, so don?t look for unsigned size of the boolean type is not explicitly specified。// Only x available{ int q = 96。double d。 Then x will get some arbitrary value (as in C and C++)。 } vo