首页 > 其他 > 详细

Chapter 7 Tokens, Values, and Variables

时间:2014-02-28 17:05:25      阅读:528      评论:0      收藏:0      [点我收藏+]

1) The tokenizer is a greedy tokenizer. It grabs as many characters as it can to build up the next token, not caring if this creates an invalid seuenceof tokens.

public class Test {
	public static void main(String[] args) {
		int i = 1;
		int a = i+++i; // equals to: i++ + i
		System.out.println(a);
	}
}

 

2) Every type (primitive or reference) has an associated instance of class Class that represents that type. These instances are often referred to as the class object for a given type. You can name the class object for a type directly by following the type name with ".class", as in

   String.class
   java.lang.String.class
   java.util.Iterator.class
   boolean.class
Since class Class is generic, the actual type of the class literal for a reference type T is Class<T>, while for primitive types it is Class<W> where W is the wrapper class for 
that primitive type. But note, for example, that boolean.class and Boolean.class are two different objects of type Class<Boolean>.
Example:
public class ClassObject {
	public static void main(String[] args) {
		System.out.println(java.lang.String.class.toString());
	}
}
Output:
class java.lang.String
 

3) You can defer the initialization of a final field or local variable. Such a final variable is called a blank final. A blank final field must be initialized within an initialization block or constructor (if it‘s an instance field) while a blank final local variable, like any local variable, must be initialized before it is used.

  The compiler will verify that all static final fields are initialized by the end of any static initializer blocks, and that non-static final fields are initialized by the end of all construction paths for an object. A compile-time error will occur if the compiler cannot determine that this happens.

 

4) An array object’s length is fixed at its creation and cannot be changed. But a new array of a different size could be assigned to the array variable at any time.

int[] ia = new int[3];
int ia[] = new int[3];

the previous two statements are equivalent, however, the former one is preferable.

 

  The important thing to remember is that the modifiers apply to the array variable not to the elements of the array the variable references.

 

5) Multidimensional Array:

float[][] mat = new float[4][4];

The first(left-most) dimension of an array must be specified when the array is created. Other dimensions can be left unspecified, to be filled in later.

float[][] mat = new float[4][];
for (int y = 0; y < mat.length; y++)
    mat[y] = new float[4];

6) Arrays are implicit extensions of Object. Given a class X, classes Y and Z that extend X, and arrays of each, the class hierarchy looks something like this:

bubuko.com,布布扣

  This class relationship allows polymorphism for arrays. You can assign an array to a variable of type Object and cast it back. An array of objects of type Y is usable wherever an array of objects of its supertype X is required. This seems natural but can require a run time check that is sometimes unexpected. An array of X can contain either Y or Z references, but an array of Y cannot contain references to X or Z objects. The following code would generate an ArrayStoreException at run time on either of its final two lines, which violate this rule:

Y[] yArray = new Y[3];      // a Y array
X[] xArray = yArray;        // valid: Y is assignable to X
xArray[0] = new Y();
xArray[2] = new X();        // INVALID: can‘t store X in Y[]
xArray[1] = new Z();        // INVALID: can‘t store Z in Y[]

  Like any other object, arrays are created and are subject to normal garbage collection mechanisms. They inherit all the methods of Object and additionally implement the Cloneable interface and the Serializable interface.

 

7)

To avoid confusion, hiding is not permitted in nested scopes within a code block. This means that a local variable in a method cannot have the same name as a parameter of that method; that a for loop variable cannot have the same name as a local variable or parameter; and that once there is a local variable called, say, über, you cannot create a new, different variable with the name über in a nested block.

Example 1:

{
    int über = 0;
    {
        int über = 2; // INVALID: already defined
        // ...
    }
}

Example 2:

void print(int val) {
		for(int i = 0; i < 100; ++i){
			int val = 100; //invalid: Duplicate local variable val
		}
	}

Example 3:

void print() {
		int val = 1;
		for(int i = 0; i < 100; ++i){
			int val = 100; //invalid: Duplicate local variable val
		}
	}

Example 4:

Chapter 7 Tokens, Values, and Variables,布布扣,bubuko.com

Chapter 7 Tokens, Values, and Variables

原文:http://javabeta.blog.51cto.com/4039428/1364525

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!