【正文】
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。 The DataOnly class cannot do much of anything except hold data, because it has no methods. To understand how those work, you must first understand arguments and return values, which will be described shortly. Default values for primitive members When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it: The default values are only what Java guarantees when the variable is used as a member of a class. This ensures that member variables of primitive types will always be initialized (something C++ doesn?t do), 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。 you can use these for accurate moary calculations, for example. Consult the JDK documentation for details about the constructors and methods you can call for these two classes. Arrays in Java Virtually all programming languages support some kind of 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 memory block or uses the memory before initialization (mon programming errors), there will be unpredictable results. 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 Thinking in Java be accessed outside of its range. The range checking es 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 are worth the expense (and Java can sometimes optimize these operations). 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 keyword: 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 null, the problem will be reported at run time. Thus, typical array errors are prevented in Java. You can also create an array of primitives. Again, the piler guarantees initialization because it zeroes the memory for that will be covered in detail in later chapters. You never need todestroy an object In most programming languages, the concept of the lifetime of a variable occupies a significant portion of the programming effort. 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 all the cleanup work for you. Scoping Most procedural languages have the concept of scope. This determines both the visibility and lifetime of the names defined within that scope. In C, C++, and Java, scope is determined by the placement of curly braces {}. So for example: { int x = 12。因此,沒(méi)有必要到方法結(jié)束時(shí)才離開(kāi),可在任何地方返回。像 Java 中任何傳遞對(duì)象的場(chǎng)合一樣,這里傳遞的實(shí)際上也是引用,并且引用類型必須正確。參數(shù)列表給出了要傳遞方法的信息的類型和名稱。 基本成員默認(rèn)值 若累的某個(gè) 成員是基本數(shù)據(jù)類型,即使沒(méi)有進(jìn)行初始化, Java 也會(huì)確保它獲得一個(gè)默認(rèn)值, 當(dāng)變量作為類的成員使用時(shí), Java 才確保給定其默認(rèn)值,以確保那些是基本類型的成員變量得到初始化 ( C++沒(méi)有 此功能 ) ,防止產(chǎn)生程序錯(cuò)誤。 字段可以是任何類型的對(duì)象,可以通過(guò)其引用與其進(jìn)行通信;也可以是基本類型中的一種。如果 Java 讓對(duì)象繼續(xù)存在,那么靠什么才能防止這些對(duì)象填滿內(nèi)存空間,進(jìn)而阻塞你的程序呢?這正是 C++里可能會(huì)發(fā)生的問(wèn)題。 盡管 以下 代碼在 C和 C++中是合法的,但是在 Java 中卻不能這樣寫: { int x = 12。因此,常犯的數(shù)組錯(cuò)誤在 Java 中就可以避免。由于這么做復(fù)雜了許多,所以運(yùn)算速度會(huì)比較慢。這個(gè)變量直接存儲(chǔ)“值”,并置于堆棧中 ,因此更加高效。這樣做是安全的,因?yàn)?他們永遠(yuǎn)都不會(huì)改變。 ( 2)堆棧。通常,必須為對(duì)象使用一種更通用的初始化 方法 。您曾利用一些特殊語(yǔ)法直接操作過(guò)對(duì)象,或處理過(guò)一些間接表示的對(duì)象嗎( C或 C++里的指針)? 所有這些在 Java 里都得到了簡(jiǎn)化,任何東西都可看作對(duì)象。 — Ludwig Wittgenstein(18891951) “盡管以 C++為基礎(chǔ),但 Java 是一種更純粹的面向?qū)ο蟪绦蛟O(shè)計(jì)語(yǔ)言”。只要握住這個(gè)遙控板,就相當(dāng)于掌握了與電視機(jī)連接的通道。 它不僅指出“ 給我一個(gè)新的字符串 ”,也通過(guò)提供一個(gè)初始字符串 ,指出了“如何生成這個(gè)新字串”。這是由于它必須生成相應(yīng)的代碼,以便向上和向下移動(dòng)指針。對(duì)于流式對(duì)象,對(duì)象會(huì)轉(zhuǎn)化成字節(jié)流,通常會(huì)發(fā)給另一臺(tái)機(jī)器。 Boolean 類型所占存儲(chǔ)空間的大小沒(méi)有明確指定,僅定義為能夠 取 字面值true 或 false。 關(guān)于調(diào)用這兩個(gè)類的構(gòu)造器和方法的詳細(xì)信息,請(qǐng)查閱 JDK 文檔。變量需要存活多長(zhǎng)時(shí)間?如 果想要銷毀對(duì)象,那什么時(shí)刻進(jìn)行呢?變量生命周期的混亂往往會(huì)導(dǎo)致大量的程序 bug,本節(jié)將介紹 Java 是怎樣替我們完成所有的清理工作,從而大大簡(jiǎn)化這個(gè)問(wèn)題的。 對(duì)象的作用域 Java 對(duì)象不具備和基本類型一樣的生命周期。你只需要?jiǎng)?chuàng)建對(duì)象,一旦不再需要,他們就會(huì)自行消失。 Boolean b。 那么變量 x 得到的可能是任意值(與 C和 C++中一樣),而不會(huì)被自動(dòng)初始化為零。通過(guò)對(duì)象調(diào)用方法時(shí),需要先列出對(duì)象名,緊接著句點(diǎn),然后是方法名和參數(shù)列表。此方法的參數(shù)類型是 String,參數(shù)名是 s。 附件 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。 // 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)。 } 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.)