【正文】
} void nothing2() {} When the return type is void, then the return keyword is used only to exit the method, and is therefore unnecessary when you reach the end of the method. You can return from a method at any point, but if you?ve given a nonvoid return type, then the piler will force you (with error messages) to return the appropriate type of value regardless of where you return. At this point, it can look like a program is just a bunch of objects with methods that take other objects as arguments and send messages to those other objects. That is indeed much of what goes on, but in the following chapter you?ll learn how to do the detailed lowlevel work by making decisions within a method. For this chapter, sending messages will suffice. 。 } 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。 For example, suppose you have a method f( ) that takes no arguments and returns a value of type int. Then, if you have an object called a for which f( ) can be called, you can say this:int x = ()。 Then x will get some arbitrary value (as in C and C++)。 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。 = 。} This class doesn?t do anything except hold data. But you can create an object like this: DataOnly data = new DataOnly()。double d。 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。 // 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)。 q available }// Only x available// q is out of scope } A variable defined within a scope is available only to the end of that text after a ?//? to the end of a line is a makes Java code easier to read. Since Java is a freeform language, the extra spaces, tabs, and carriage returns do not affect the resulting cannot do the following, even though it is legal in C and C++:{ int x = 12。// Only x available{ int q = 96。 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。 Java SE5 autoboxing will automatically convert from a primitive to a wrapper type: Character ch = ?x?。 Character ch = new Character(c)。 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 othe