#jls-2# The class File Format, class文件格式

小结:
4.3 Reference Types and Values
4.3.1 Objects
4.4 Type Variables
4.5 Parameterized Types 参数化类型
4.7 Reifiable Types
4.8 Raw Types 无类型/自定义类型
4.11 Where Types Are Used
4.12 Variables
4.12.3 Kinds of Variables
4.12.4 final Variables
4.12.5 Initial Values of Variables
4.12.6 Types, Classes, and Interfaces

4.3 Reference Types and Values

four kinds of reference types: (这里用jshell写分块就会学得很清楚)
class types (§8.1),
interface types (§9.1),
type variables (§4.4),
and array types (§10.1).

//The sample code:
class Point { int[] metrics; }
interface Move { void move(int deltax, int deltay); }

4.3.1 Objects

what: An object is a class instance or an array.

If two variables contain references to the same object, the state of the object can be modified using one variable's reference to the object, and then the altered state can be observed through the reference in the other variable.

//Example 4.3.1-2. Primitive and Reference Identity
class Value { int val; }
class Test {
 public static void main(String[] args) {
 int i1 = 3;
 int i2 = i1;
 i2 = 4;
 System.out.print("i1==" + i1);
 System.out.println(" but i2==" + i2);
 Value v1 = new Value();
 v1.val = 5;
 Value v2 = v1;  //point to same object
 v2.val = 6;
 System.out.print("v1.val==" + v1.val);
 System.out.println(" and v2.val==" + v2.val);
 }
}

//This program produces the output:
i1==3 but i2==4
v1.val==6 and v2.val==6

在jshell看分配的内存地址,显示更清晰。以下v3,v4是指向同一个变量。t1,t2则是创建了两个不同的object。

//point to the same object
jshell> Value v3 = new Value()
v3 ==> Value@598067a5

jshell> Value v4 = v3
v4 ==> Value@598067a5

//create different objects 
jshell> Value t1 = new Value()
t1 ==> Value@14bf9759

jshell> Value t2 = new Value()
t2 ==> Value@553f17c

4.4 Type Variables

以下的例子可以看出作用范围。private仅用于class C,其他不可调用。

class C {
    public void mCPublic() {}
    protected void mCProtected() {}
    void mCPackage() {}
    private void mCPrivate() {}
}
interface I {
    void mI();
}
class CT extends C implements I {
    public void mI() {}
} 

class Test {
     void test(T t) {
        t.mI(); // OK
        t.mCPublic(); // OK
        t.mCProtected(); // OK
        t.mCPackage(); // OK
        t.mCPrivate(); // Compile-time error
    }
}

4.5 Parameterized Types 参数化类型

在method中限定了参数类型。

//Example 4.5.1-1. Unbounded Wildcards

import java.util.ArrayList;
import java.util.Collection;

class Test{

    static void printCollection(Collection c) {
        for (Object o : c) {
            System.out.println(o);
        }
    }

    public static void main(String[] args) {
        Collection cs = new ArrayList();
        cs.add("hello");
        cs.add("leemin");
        printCollection(cs);
    }

}

//output 
hello
leemin

4.7 Reifiable Types

因为在编译期间会删除某些类型信息,所以不是所有类型都是在运行时可用。 在运行时完全可用的类型是已知的作为可再生类型。

4.8 Raw Types 无类型/自定义类型

看定义有点不太懂,但是看例子是自定义的类型;不一定需要是java自带类型。我觉得像这样的小例子在IDEA 写class(有提示)比较简单,然后在jshell看会很直观。

//Example 4.8-1. Raw Types

class Cell {
    E value;

    Cell(E v) { value = v; }
    E get()   { return  value; }
    void set(E v) {value = v;}
}

//output 

jshell> Cell x = new Cell("abc");
x ==> Cell@13deb50e

jshell> x.value
$8 ==> "abc"

jshell> x.get()
$9 ==> "abc"

jshell> x.set("def")
|  警告:
|  对作为原始类型Cell的成员的set(E)的调用未经过检查
|  x.set("def")
|  ^----------^

Example 4.8-2. 只能用来看继承关系,不能真实运行。因为会抛出null异常。

//Example 4.8-2. Raw Types and Inheritance
import java.util.*;
class NonGeneric {
    Collection myNumbers() { return null; }
}
abstract class RawMembers extends NonGeneric
        implements Collection {
    static Collection cng =
            new ArrayList();
    public static void main(String[] args) {
        RawMembers rw = null;
        Collection cn = rw.myNumbers();
        // OK
        Iterator is = rw.iterator();
        // Unchecked warning
        Collection cnn = rw.cng;
        // OK, static member
    }
}

//try output 
jshell> rw.myNumbers()
|  java.lang.NullPointerException thrown
|        at (#24:1)

jshell> rw.iterator()
|  java.lang.NullPointerException thrown
|        at (#25:1)

4.11 Where Types Are Used

光是介绍就有十六种我真的是很服气的.........
Types are used in most kinds of declaration and in certain kinds of expression.Specifically, there are 16 type contexts where types are used:

//Example 4.11-1. Usage of a Type

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

class MiscMath{
    int divisor;
    MiscMath(int divisor){this.divisor = divisor;}
    float ratio(long l){
        try{
            l /=divisor;
        }catch (Exception e){
            if (e instanceof ArithmeticException)
                l = Long.MAX_VALUE;
            else
                l = 0;
        }
        return (float)l;
    }

    double gausser(){
        Random r = new Random();
        double[] val = new double[2];
        val[0] = r.nextGaussian();
        val[1] = r.nextGaussian();
        return (val[0] + val[1])/2 ;
    }

    Collection fromArray(Number[] na){
        Collection cn = new ArrayList();
        for (Number n : cn) cn.add(n);
        return cn;
    }

     void loop(S s) { this.loop(s);}


}

4.12 Variables

4.12.3 Kinds of Variables

//Example 4.12.3-1. Different Kinds of Variables
class Point {
    static int numPoints; // numPoints is a class variable
    int x, y; // x and y are instance variables
    int[] w = new int[10]; // w[0] is an array component
    int setX(int x) { // x is a method parameter
        int oldx = this.x; // oldx is a local variable
        this.x = x;
        return oldx;
    }
}

//output 
jshell> Point testPoint = new Point()
testPoint ==> Point@1ce92674

jshell> testPoint.x
$17 ==> 0

jshell> testPoint.y
$18 ==> 0

jshell> testPoint.w
$19 ==> int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

//output after setX() method
jshell> testPoint.setX(99)
$20 ==> 0

jshell> testPoint.x
$21 ==> 99

jshell> testPoint.y
$22 ==> 0

jshell> testPoint.w
$23 ==> int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

4.12.4 final Variables

变量可以声明为final。 最终变量只能分配一次。

声明变量final可以作为有用的记录,其值不会改变
并可以帮助避免编程错误。

  1. Point.origin can never change
  2. Point object might change its state
//Example 4.12.4-1. Final Variables

class PointFinal{
    int x,y;
    int useCount;
    PointFinal(int x, int y){this.x = x;this.y = y;}
    static final PointFinal origin = new PointFinal(0,0);
}

//output for final var 
jshell> PointFinal.origin.x
$34 ==> 0

jshell> PointFinal.origin.y
$35 ==> 0

//output for a new PointFinal object
jshell> PointFinal ft = new PointFinal(6,7)
ft ==> PointFinal@4b952a2d

jshell> ft.x
$37 ==> 6

jshell> ft.y
$38 ==> 7

jshell> ft.or
jshell> ft.useCount
$39 ==> 0

4.12.5 Initial Values of Variables

初始值: 数值类型是不同格式的零值;字符和reference类型是null值,布尔类型是false

– For type byte, the default value is zero, that is, the value of (byte)0.
– For type short, the default value is zero, that is, the value of (short)0.
– For type int, the default value is zero, that is, 0.
– For type long, the default value is zero, that is, 0L.
– For typefloat, the default value is positive zero, that is, 0.0f.
– For type double, the default value is positive zero, that is, 0.0d.
– For type char, the default value is the null character, that is, '\u0000'
– For type boolean, the default value is false.
– For all reference types (§4.3), the default value is null.

//Example 4.12.5-1. Initial Values of Variables

class PointInit{
    static int npoint;
    int x , y;
    PointInit root;
}

//output 
jshell> PointInit a = new PointInit()
a ==> PointInit@5025a98f

jshell> a.x
$43 ==> 0

jshell> a.y
$44 ==> 0

jshell> a.root
$45 ==> null

4.12.6 Types, Classes, and Interfaces

//Example 4.12.6-1. 
//Type of a Variable versus Class of an Object

interface Colorable {
    void setColor(byte r, byte g, byte b);
}
class Point { int x, y; }
class ColoredPoint extends Point implements Colorable {
    byte r, g, b;
    public void setColor(byte rv, byte gv, byte bv) {
        r = rv; g = gv; b = bv;
    }
}
class Test {
    public static void main(String[] args) {
        Point p = new Point();
        ColoredPoint cp = new ColoredPoint();
        p = cp;
        Colorable c = cp;
    }
}

小结: java se (ver 10) language specification
学习进度:Absolute page 69-110

2018.7.6

你可能感兴趣的:(#jls-2# The class File Format, class文件格式)