Java语言规范第四章-类型、值、变量

来源:互联网 发布:黑豹伪装m10数据 编辑:程序博客网 时间:2024/06/06 15:35

Java范第四章-型、(Java Language Specification – Chapter4 Types,Values,Variables)

 

如果整形运算符的操作数包含long型,那么该操作按照64位精度算,果也是long型。如果其他操作数不是long型,那先将其long型。其他情况下,操作都按照32位精度算,int型,非int型的操作数需要int型。 

 

内置的整型运算符在任何情况下都不会提示是否溢出。整型操作可能生的异常如下:

NullPointerException:拆箱(unboxing)操作(Type.typeValue())

ArithmeticException:除法(/)与求余(%)运算符

OutOfMemoryError:装箱(boxing)操作

class Test {

        public static void main(String[] args) {

                int i = 1000000;

                System.out.println(i * i);

                long l = i;

                System.out.println(l * l);

                System.out.println(20296 / (l - i));

        }

}

:

-727379968

1000000000000

然后会抛出ArithmeticException异常,因l – i = 0。第一个乘法操作是按照32位精度算的,-727379968 是数学果的低32位十制数,因1000000000000超出了int的范

 

class Point {

        int x, y;

        Point() { System.out.println("default"); }

        Point(int x, int y) { this.x = x; this.y = y; }

        // A Point instance is explicitly created at class initialization time:

        static Point origin = new Point(0,0);

        // A String can be implicitly created by a + operator:

        public String toString() {

                return "(" + x + "," + y + ")";

        }

}

class Test {

        public static void main(String[] args) {

                // A Point is explicitly created using newInstance:

                Point p = null;

                try {

                        p = (Point)Class.forName("Point").newInstance();

                } catch (Exception e) {

                        System.out.println(e);

                }

                // An array is implicitly created by an array constructor:

                Point a[] = { new Point(0,0), new Point(1,1) };

                // Strings are implicitly created by + operators:

                System.out.println("p: " + p);

                System.out.println("a: { " + a[0] + ", "

                                                                                   + a[1] + " }");

                // An array is explicitly created by an array creation expression:

                String sa[] = new String[2];

                sa[0] = "he"; sa[1] = "llo";

                System.out.println(sa[0] + sa[1]);

        }

}

 

可以用于引用的操作符:

    * 成员访问

    * 方法调用

    * 强制转换

    * 字符串连接

    * 类型判断(instanceof)

    * 引用比较==!=

    * 条件运算符? :

 

两个变量同时引用同一个对象时,通过一个修改一个变量的值来修改对象的成员。

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);//3

                System.out.println(" but i2==" + i2);//4

                Value v1 = new Value();

                v1.val = 5;

                Value v2 = v1;

                v2.val = 6;

                System.out.print("v1.val==" + v1.val);//6

                System.out.println(" and v2.val==" + v2.val);//6

        }

}

 

参数类型示例:

    Vector<String>

    Seq<Seq<A>>

    Seq<String>.Zipper<Integer>

    Collection<Integer>

    Pair<String,String>

 

    // Vector<int> -- illegal, primitive types cannot be arguments

    // Pair<String> -- illegal, not enough arguments

    // Pair<String,String,String> -- illegal, too many arguments

TypeArguments:

        < ActualTypeArgumentList >

ActualTypeArgumentList:

        ActualTypeArgument

        ActualTypeArgumentList , ActualTypeArgument

ActualTypeArgument:

        ReferenceType

        Wildcard

Wildcard:

        ? WildcardBoundsOpt

Wildcards are useful in situations where only partial knowledge about the type parameter is required.

WildcardBounds:

        extends ReferenceType

        super ReferenceType

 

void printCollection(Collection<?> c)

public Method getMethod(Class<?>[] parameterTypes)

boolean addAll(Collection<? extends E> c)

Reference(T referent, ReferenceQueue<? super T> queue);

 

It is possible that future versions of the Java programming language will disallow the use of raw types.

List a = new ArrayList();

may be disallowed in the future version of Java.

 

可以将Vector<String>赋值给Vector,但是反过来的赋值时不安全的。但是为了保持兼容性,仍然允许,可以使用unchecked来进行标记。 

除非使用了unchecked警告,否则Java变成可以保证变量的类型和变量的值兼容,这种保证的一个例外是数组,数组的类型检查时在运行时进行的。

除非使用了unchecked警告,否则Java变成可以保证变量的类型和变量的值兼容,这种保证的一个例外是数组,数组的类型检查时在运行时进行的。

class Point { int x, y; }

class ColoredPoint extends Point { int color; }

class Test {

              public static void main(String[] args) {

                            ColoredPoint[] cpa = new ColoredPoint[10];

                            Point[] pa = cpa;

                            System.out.println(pa[1] == null);

                            try {

                                          pa[0] = new Point();

                            } catch (ArrayStoreException e) {

                                          System.out.println(e);

                            }

              }

}

输出结果如下:

true

java.lang.ArrayStoreException

package org.jff.jls.ch4;

import java.util.ArrayList;

import java.util.List;

public class FinalTest {

       private static final List<String> STUDENTS = new ArrayList<String>();

       public static void main(String[] args) {

              STUDENTS.add("hello world");

              for (String student : STUDENTS) {

                     System.out.println(student);//hello

              }

       }

}

原创粉丝点击