【正文】
就知道這個(gè)引用還沒(méi)有指向某個(gè)對(duì)象。 Java 中的數(shù)組 幾乎所有的程序設(shè)計(jì)語(yǔ)言都支持?jǐn)?shù)組。也就是說(shuō),能作用于 int 或 float 的操作,也能作用于 BigInteger 或 Big Decimal?;绢愋途哂械陌b器類,使得可以在堆中創(chuàng)建一個(gè)非基本對(duì)象,用來(lái)表示對(duì)應(yīng)的基本類型。因此,對(duì)于這些類型, Java 采取與 C 和 C++相同的方法。而對(duì)于持久化 對(duì)象,對(duì)象保存在磁盤中。 ( 4) 常量 存儲(chǔ)。這一限制無(wú)疑影響了程序的靈活性,所以盡管有些Java 數(shù)據(jù)要保存在堆棧里 —— 特別是對(duì)象句柄,但 Java 對(duì)象并不 存儲(chǔ)于 其中。然而,寄存器的數(shù)量十分有限,所以寄存器是根據(jù)需要由編譯器分配。 當(dāng)然,字串( String)并非唯一的類型。因此,一種更安全的做法是:創(chuàng)建一個(gè)句柄時(shí),記住無(wú)論如何都進(jìn)行初始化: String s = “ zyp” 。但一旦需要“換頻道”或者“關(guān)小聲音”,我們實(shí)際操縱的是遙控板(句柄),再有遙控板自己操縱電視機(jī)(對(duì)象)。 用 句柄操縱對(duì)象 每種編程語(yǔ)言都有自己的數(shù)據(jù)處理方式。 無(wú)論 C++還是 Java 都屬于 雜合語(yǔ)言 。但在 Java 中,設(shè)計(jì)者覺(jué)得這種雜合并不像在 C++里那么重要。有些時(shí)候,程序員必須時(shí)刻留意準(zhǔn)備處理的是什么類型。如果要在房間里四處走走,并想保持對(duì)電視機(jī)的控制,那么手上拿著的是遙控板,而非 電視機(jī)。 然而,這里用到了 Java 語(yǔ)言的一個(gè)特性 :字串可以用加引號(hào)的文本 初始化 。 Java 配套提供了數(shù)量 眾 多的現(xiàn)成類型。我們對(duì)此沒(méi)有直接的控制權(quán),也不可能在自己的程序里找到寄存器存在的任何蹤跡 (另一方面, C 和C++允許您向編譯器建議寄存器的分配方式) 。 ( 3)堆。常數(shù)值通常直接置于程序代碼內(nèi)部。即使程序中止運(yùn)行,他們?nèi)钥杀3肿约旱臓顟B(tài)不變。也就是說(shuō),不用 new 來(lái)創(chuàng)建變量,而是創(chuàng)建一個(gè)并非是引用的“自動(dòng)”變量。例如: Char c = ‘ x’ 。 只不過(guò)必須以 方法調(diào)用方式取代運(yùn)算符方式來(lái)實(shí)現(xiàn)。在 C 和 C++中使用數(shù)組是很危險(xiǎn)的,因?yàn)?C 和C++中的數(shù)組就是內(nèi)存塊。在使用任何引用前,必須為其指定一個(gè)對(duì)象;如果試圖使用一個(gè)還是 null 的引用,在運(yùn)行時(shí)將會(huì)報(bào)錯(cuò)。作用域決定了在其內(nèi)定義的變量名的可見(jiàn)性和生命周期。由于 Java 是一種自由格式( freeform)的語(yǔ)言,所以,空格、制表符、換行都不會(huì)影響程序的執(zhí)行結(jié)果。所以假如你采用代碼 { String s = new String(“ a string” )。 這樣便帶來(lái)一個(gè)有趣的問(wèn)題。 創(chuàng)建新的數(shù)據(jù)類型:類 如果一切都是對(duì)象,那么是什么決定了某一類對(duì)象的外觀與行為呢?換句話說(shuō),是什么確定了對(duì)象的類型?你可能期望有一個(gè)名為“ type” 的關(guān)鍵字 ,當(dāng)然它必須還要有相 應(yīng)的含義。 字段和方法 一旦定義了一個(gè)類(在 Java 中你所做的全部工作就是定義類,產(chǎn)生那些類的對(duì)象,以及發(fā)送消息給這些對(duì)象),就可以在類中設(shè)置兩種類型的元素:字段(有時(shí)被稱作數(shù)據(jù)成員)和方法(有時(shí)被稱作成員函數(shù))。 可以給字段賦值,但首先必須知道如何引用一個(gè)對(duì)象的成員。如果想了解成員 方法的運(yùn)行機(jī)制,就得先了解參數(shù)和返回值的概念,稍后將對(duì)此作簡(jiǎn)略描述。如果忘記了這么做, Java 會(huì)在編譯時(shí)返回一個(gè)錯(cuò)誤。下面是它最基本的形式: ReturnType methodName(/*Argument list*/){ /* Method body*/ } 返回類型描述的是在調(diào)用方法之后從方法返回的值。 例如 ,假設(shè)有一個(gè)方法 f(),不帶任何參數(shù),返回類型是 int。因此,在參數(shù)列表中必須指定每個(gè)所傳遞對(duì)象的類型及名字。在這里, s 的 length()方法被調(diào)用,它是 String 類提供的方法之一,會(huì)返回字符串 包含的字符數(shù)。} void nothing2(){} 若返回類型是 void,return 關(guān)鍵字的作用只是用來(lái)推出方法。However, this uses a special Java feature: Strings can be initialized with quoted text. Normally, you must use a more general type of initialization for objects. You must create all the objects When you create a reference, you want to connect it with a new object. You do so, in general, with the new operator. 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)。 The reasons for wrapping primitives will be shown in a later chapter. Highprecision numbers Java includes two classes for performing highprecision arithmetic: BigInteger and BigDecimal. Although these approximately fit into the same category as the “wrapper” classes, neither one has a primitive 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 that 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. BigInteger supports arbitraryprecision integers. This means that you can accurately represent integral values of any size without losing any information during operations. BigDecimal is for arbitraryprecision fixedpoint numbers。 Fields and methods When you define a class (and all you do in Java is define classes, make objects of those classes, and send messages to those objects), you can put two types of elements in your class: fields (sometimes called data members), and methods (sometimes called member functions). A field is an object of any type that you can talk to via its reference, or a primitive type. If it is a reference to an object, you must initialize that reference to connect it to an actual object (using new, as seen earlier). Each object keeps its own storage for its fields。 It is also possible that your object might contain other objects that contain data you?d like to modify. For this, you just keep “connecting the dots.” For example: = 100。 } double naturalLogBase() { return 。 } This method tells you how many bytes are required to hold the information in a particular String. (The size of each char in a String is 16 bits, or two bytes, to support Unicode characters.) The argument is of type String and is called s. Once s is passed into the method, you can treat it just like any other object. (You can send messages to it.) Here, the length( ) method is called, which is one of the methods for Strings。 = 。 // Illegal}}The piler will announce that the variable x has already been defined. Thus the C and C++ ability to “hide” a variable in a larger scop e is not allowed, because the Java designers thought that it led to confusing programs. Scope of objects 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:{String s = new String(a string)。 Java SE5 autoboxing will automatically convert from a primitive to a wrapper type: Character ch = ?x?。 附件 2:外文 原文 Everything Is an Object “If we spoke a different language, we would perceive a some what different world.” Ludwig Wittgenstein (18891951)Although it is based on C++, Java is more of a “pure” objectoriented language. 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++. A hybrid language allows multiple programming styles。} double naturalLogBase(){return 。此方法的參數(shù)類型是 String,參數(shù)名是 s。 參數(shù)列表 方法的參數(shù)列表指定要傳遞給方法什么樣的信息。通過(guò)對(duì)象調(diào)用方法時(shí),需要先列出對(duì)象名,緊接著句點(diǎn),然后是方法名和參數(shù)列表。 Java 的方法決定了一個(gè)對(duì)象能 夠接收什么樣的消息。 那么變量 x 得到的可能是任意值(與 C和 C++中一樣),而不會(huì)被自動(dòng)初始化為零。例如: = 100。 Boolean b。 ATypeName a = new ATypeName()。你只需要?jiǎng)?chuàng)建對(duì)象,一旦不再需要,他們就會(huì)自行消失。這樣,許多 C++編程問(wèn)題在 Java 中就完全消失了。 對(duì)象的作用域 Java 對(duì)象不具備和基本類型一樣的生命周期。 任何位于“ //”之后到行末的文字都是注釋。變量需要存活多長(zhǎng)時(shí)間?如 果想要銷毀對(duì)象,那什么時(shí)刻進(jìn)行呢?變量生命周期的混亂往往會(huì)導(dǎo)致大量的程序 bug,本節(jié)將介紹 Java 是怎樣替我們完成所有的清理工作,從而大大簡(jiǎn)化這個(gè)問(wèn)題的。 當(dāng)創(chuàng)建一個(gè)數(shù)組對(duì)象時(shí),實(shí)際上就是創(chuàng)建了一個(gè)引用數(shù)組,并且每個(gè)引用都會(huì)自動(dòng)初始化為一個(gè)特定值,該 值擁有自己的關(guān)鍵字 null。 關(guān)于調(diào)用這兩個(gè)類的構(gòu)