首页 > 代码库 > 一些Java相关的
一些Java相关的
都是从《Thinking in Java》英文第四版中摘抄的
_______________________________________________________________________________________________________________________
变量初始化:
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, 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.
(也就是方法内部定义的变量,我试了一下,如果只声明不初始化也不使用的话,不会报错,会报警告,如果使用的话,编译时报错)
_______________________________________________________________________________________________________________________
sizeof:
Java has no "sizeof"
In C and C++, the sizeof() operator tells you the number of bytes allocated for data items. The most compelling reason for sizeof() in C and C++ is for portability(可移植性). Different data types might be different sizes on different machines(参看http://www.zhihu.com/question/19580654), so the programmer must discover how big those types are when performing operations that are sensitive to size.For example, one computer might store integers in 32 bits, whereas another might store integers as 16 bits. Programs could store larger values in integers on first machine. As you might imagine, portability is a huge headache for C and C++ programmers.
Java does not need a sizeof() operator for this purpose, because all the data types are the same on all machines. You do not need to think about portability on this level--it is designed into the language.
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 languages.
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.
_______________________________________________________________________________________________________________________
if while 的判断条件必须是boolean,不能是int
while(x = y) {
// ....
}
The programmer was clearly tryingto test for equivalence (==) rather than do an assignment. In C and C++ the result of this assignment will always be true if y is nonzero, and you’ll probably get an infinite loop. In Java, the result of this expression is not a boolean, but the compiler expects a boolean and won’t convert from an int, so it will conveniently give you a compile-time error and catch the problem before you ever try to run the program. So the pitfall never happens in Java. (The only time you won’t get a compiletime error is when x and y are boolean, in which case x = y is a legal expression, and in the preceding example, probably an error.)
All conditional statements use the truth or falsehood of a conditional expression to determine the execution path. An example ofa conditional expression is a == b. This uses the conditional operator == to see if the value of ais equivalent to the value of b. The expression returns trueor false. Any of the relational operators you’ve seen in the previous chapter can be used to produce a conditional statement. Notethat Java doesn’t allow you to use a number as a boolean, even though it’s allowed in C and C++ (where truth is nonzero and falsehood is zero). If you want to use a non-booleanin a booleantest, such as if(a), you must first convert it to a boolean value by using a conditional expression, such as if(a != 0).
_______________________________________________________________________________________________________________________
goto:
Although goto is a reserved word in Java, it is not used in the language; Java has no goto.
_______________________________________________________________________________________________________________________
return
You can also see the use of the return keyword, which does two things. First, it means “Leave the method, I’m done.” Second, if the method produces a value, that value is placed right after the return statement.
The returnkeyword has two purposes: It specifies what value a method will return (if it doesn’t have a void return value) and it causes the current method to exit, returning that value.
_______________________________________________________________________________________________________________________
constructor
The constructor is an unusual typeof method because it has no return value. This is distinctly different from a voidreturn value, in which the methodreturns nothing but you still have the option to make it return something else. Constructors return nothing and you don’t have an option (the newexpression does return a reference to the newly created object, but the constructor itself has no return value).
The expression new Bird() creates a new object and calls the default constructor, even though one was not explicitly defined. Without it, you would have no method to call to build the object. However, if you define any constructors (with or without arguments), the compiler will notsynthesize one for you.
_______________________________________________________________________________________________________________________
overloaded 参数类型不同,返回值不同是不可以的
If the methods have the same name, how can Java know which method you mean? There’s a simple rule: Each overloaded method must take a unique list of argument types. If you think about this for a second, it makes sense. How else could a programmer tell the difference between two methods that have the same name, other than by the types of their arguments? Even differences in the ordering of arguments are sufficient to distinguish two methods, although you don’t normally want to take this approach because it produces difficult-tomaintain code.
It is common to wonder, “Why only class names and method argument lists? Why not distinguish between methods based on their return values?” For example, these two methods, which have the same name and arguments, are easily distinguished from each other:
void f() {}
int f() { return 1; }
This might work fine as longas the compiler could unequivocally determine the meaning from the context(我试了,编译器报错了), as in int x = f( ). However, you can also call a method and ignore the return value. This is often referred to as calling a method for its side effect, since you don’t care about the return value, but instead want the other effects of the method call. So if you call the method this way:
f();
how can Java determine which f( ) should be called? And how could someone reading the code see it? Because of this sort of problem, you cannot use return value types to distinguish overloaded methods.
_______________________________________________________________________________________________________________________
this:
1. The thiskeyword—which can be used only inside a non-static method—produces the reference to the object that the method has been called for.
2. The thiskeyword is also useful for passing the current object to another method: (这个用法不熟悉)
1 // : initialization/PassingThis.java 2 class Person { 3 public void eat(Apple apple) { 4 Apple peeled = apple.getPeeled(); 5 System.out.println("Yummy"); 6 } 7 } 8 9 class Peeler { 10 static Apple peel(Apple apple) { 11 // ... remove peel 12 return apple; // Peeled 13 } 14 } 15 16 class Apple { 17 Apple getPeeled() { 18 return Peeler.peel(this); 19 } 20 } 21 22 public class PassingThis { 23 public static void main(String[] args) { 24 new Person().eat(new Apple()); 25 } 26 } /* 27 * Output: Yummy 28 */// :~
Appleneeds to call Peeler.peel( ), which is a foreign utility method that performs an operation that, for some reason, needs to be external to Apple(perhaps the external method can be applied across many different classes, and you don’t want to repeat the code). To pass itself to the foreign method, it must use this.
3. Calling constructors from constructors
1 //: initialization/Flower.java 2 // Calling constructors with "this" 3 import static net.mindview.util.Print.*; 4 5 public class Flower { 6 int petalCount = 0; 7 String s = "initial value"; 8 9 Flower(int petals) { 10 petalCount = petals; 11 print("Constructor w/ int arg only, petalCount= " + petalCount); 12 } 13 14 Flower(String ss) { 15 print("Constructor w/ String arg only, s = " + ss); 16 s = ss; 17 } 18 19 Flower(String s, int petals) { 20 this(petals); 21 // ! this(s); // Can’t call two! 22 this.s = s; // Another use of "this" 23 print("String & int args"); 24 } 25 26 Flower() { 27 this("hi", 47); 28 print("default constructor (no args)"); 29 } 30 31 void printPetalCount() { 32 // ! this(11); // Not inside non-constructor! 33 print("petalCount = " + petalCount + " s = " + s); 34 } 35 36 public static void main(String[] args) { 37 Flower x = new Flower(); 38 x.printPetalCount(); 39 } 40 } /* 41 * Output: Constructor w/ int arg only, petalCount= 47 String & int args default 42 * constructor (no args) petalCount = 47 s = hi 43 */// :~
The constructor Flower(String s, int petals)shows that, while you can call one constructor using this, you cannot call two. In addition, the constructor call must be the first thing you do, or you’ll get a compiler error message.
In printPetalCount( ) you can see that the compiler won’t let you call a constructor from inside any method other than a constructor.(构造函数中使用this调用构造函数,不能在非构造函数中调用构造函数)
4. This example also shows another way you’ll see thisused. Since the name of the argument s and the name of the member data sare the same, there’s an ambiguity. You can resolve it using this.s, to say that you’re referring to the member data.
_______________________________________________________________________________________________________________________
Static:
With the this keyword in mind, you can more fully understand what it means to make a method static. It means that there is no this for that particular method. You cannot call non-static methods from inside static methods(although the reverse is possible), and you can call a static method for the class itself, without any object. In fact, that’s primarily what a staticmethod is for. It’s as if you’re creating the equivalent of a global method. However, global methods are not permitted in Java, and putting the static method inside a class allows it access to other static methods and to static fields.
Some people argue that static methods are not object-oriented, since they do have the semantics of a global method; with a static method, you don’t send a message to an object, since there’s no this. This is probably a fair argument, and if you find yourself using a lot of static methods, you should probably rethink your strategy. However, statics are pragmatic, and there are times when you genuinely need them, so whether or not they are “proper OOP”
should be left to the theoreticians.
When you say something is static, it means that particular field or method is not tied to any particular object instance of that class. So even if you‘ve never created an object of that class you can call a static method or access a static field. With ordinary, non-static fields and methods, you must create an object and use that object to access that fieldor method, since non-static fields and methods must know the particular object they are working with.Of course, since static methods don‘t need any objects to be created before they are used, they can‘t directly access non-static members or methods by simply calling those other members without referring to a named object(since non-static members and methods must be tied to a particular object).