Java语言规范第四章-类型、值、变量(Java Language Specification – Chapter4 Types,Values,Variables)
如果整形运算符的操作数包含long类型,那么该操作按照64位精度进行计算,结果也是long类型。如果其他操作数不是long类型,那么先将其扩展为long类型。其他情况下,操作都按照32位精度计算,结果为int类型,非int类型的操作数需要扩展为int类型。
内置的整型运算符在任何情况下都不会提示是否溢出。整型操作可能发生的异常如下:
NullPointerException:拆箱(unboxing)操作(Type.typeValue())
ArithmeticException:除法(/)与求余(%)运算符
OutOfMemoryError:装箱(boxing)操作
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
ReferenceType
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
}
}
}