Java语言规范第五/六章-数据类型转换/名称

来源:互联网 发布:最强大脑网络在线直播 编辑:程序博客网 时间:2024/06/06 00:30

Java语言规范第五章-数据类型转换(Java Language Specification – Chapter5 Conversions)

Java语言规范第六章-名称(Java Language Specification – Chapter6 Names)

 

int big = 1234567890;

float approx = big;

System.out.println(approx);// 1.23456794E9

System.out.println((int) approx);// 1234567936

在从int转换为float的过程中,数据发生了精度丢失。

 

下面的转换组合了扩展和收缩两种转换:

byte to char

首先byte被转换为int(扩展),然后再由int转换为char(收缩)


import java.util.*;

class Vector {

int val[] = { 1 , 2 };

}

class Test {

    public static void main(String[] args) {

        Vector v = new Vector();

        System.out.println(v.val[0]);//1

    }

}

java.util.Vector会被屏蔽掉。

 

package vector;

public class Vector { Object[] vec; }

由于vector包已经包含了一个Vector类,所以它不能在包含一个名为Vectorsubpackage

 

构造器和类型变量不是成员。

 

interface Colors {

    int WHITE = 0, BLACK = 1;

}

 

interface Separates {

    int CYAN = 0, MAGENTA = 1, YELLOW = 2, BLACK = 3;

}

 

class Test implements Colors, Separates {

    public static void main(String[] args) {

        System.out.println(BLACK); // compile-time error: ambiguous

    }

}

 

BLACK是歧义的 ,必须适应full qualified name来进行确定。

 

java.lang.Object:

    public final native Class<?> getClass();

jsr.TestInterface:

    public final Class<?> getClass();//modifier final not allowed here

    public Class<?> getClass();//cannot override getClass() in java.lang.Object;overriden method is final.

 

package jsr901;

public class Point {

    public int x,  y;

    void move(int dx, int dy) {

        x += dx;

        y += dy;

    }

    public void moveAlso(int dx, int dy) {

        move(dx, dy);

    }

}

 

package jsr901.sub;

public class PlusPoint extends jsr901.Point {

    public void move(int dx, int dy) {

        super.move(dx, dy);// compile-time error

        //move(int,int) is not public in jsr901.Point, can not be accessed from outside package

        moveAlso(dx, dy);

    }

}

由于在父类中没有将move定义为public或者protected,所以子类不会覆盖父类的move方法,而在调用super方法时也会发生变异错误。

如果将super.move(dx,dy)一行删除,执行下面的测试程序,程序可以正常运行,因为子类并未覆盖父类的方法,所以只是单纯的运行子类的方法:

package jsr901.sub;

class TestPoint {

    public static void main(String[] args) {

        PlusPoint pp = new PlusPoint();

        pp.move(1, 1);

        System.out.println("END");

    }

}

如果将父类中的move修改为protected或者public,那么由于调用过程是无限循环,所以将导致:Exception in thread "main" java.lang.StackOverflowError