简单说就是类型转换,分两种:“缩小转换”(Narrowing conversion),“放大转换”(Widening conversion)
1. 缩小转换
将‘大类型’ 造型成 ‘小类型’ (比如float到int)需要强制转换,可能会丢掉一部分信息(小数部分)。强制转换需要手动添加代码明确转换类型。
void narrowCast() {
long l = 2088392L;
int i = (int) l; //Narrowly convert long to int
float f = 232.2321;
byte b = (byte) f; //Narrowly convert float to byte
}
2. 放大转换
’小类型‘ 到 ’大类型‘ 造型不需要额外代码,下面两个例子不必使用(long)运算符也能自动将int转换成long类型:
void widenCast() {
int i = 200;
long l = (long)i; //Same as 'long l = i;'
long l2 = (long)200; //Same as 'long l2 = 200;'
}
3. 造型范围
Java中可在任何主类型(bollean除外,bollean不允许进行造型)之间进行造型
类不能进行造型,需用特殊方法将一个类转换成另一个类(之后博客补上)
4. 主类型
boolean 1 位 - - Boolean
char 16位 Unicode 0 Unicode 2的 16次方-1 Character
byte 8位 -128 +127 Byte
short 16 位 -2 的15 次方 +2的 15次方-1 Short
int 32位 -2的 31次方 +2 的31 次方-1 Integer
long 64位 -2 的63 次方 +2的 63次方-1 Long
float 32 位 Float
double 64 位 Double
Void - - - Void
6. 函数参数中的主类型转换(主类型过载)
重载函数中的参数能自动将 ’较小‘ 的主类型转换成 ’较大‘ 的主类型,如果想将较大类型转换成小类型,则必须显示指定转换类型,否则编译器会报错。
下例中(来自Thinking in Java 4th edition),整数常数会被当成int处理
而char比较特殊,如果没有匹配到char类型参数的method,就会转换成int
其他情况主类型都是转换成最近的比自己 ‘大’ 的主类型:byte < short < int < long < float < double
//: PrimitiveOverloading.java
// Promotion of primitives and overloading
public class HahuTest {
// boolean can't be automatically converted
static void prt(String s) {
System.out.println(s);
}
void f1(char x) { prt("f1(char)"); }
void f1(byte x) { prt("f1(byte)"); }
void f1(short x) { prt("f1(short)"); }
void f1(int x) { prt("f1(int)"); }
void f1(long x) { prt("f1(long)"); }
void f1(float x) { prt("f1(float)"); }
void f1(double x) { prt("f1(double)"); }
void f2(byte x) { prt("f2(byte)"); }
void f2(short x) { prt("f2(short)"); }
void f2(int x) { prt("f2(int)"); }
void f2(long x) { prt("f2(long)"); }
void f2(float x) { prt("f2(float)"); }
void f2(double x) { prt("f2(double)"); }
void f3(short x) { prt("f3(short)"); }
void f3(int x) { prt("f3(int)"); }
void f3(long x) { prt("f3(long)"); }
void f3(float x) { prt("f3(float)"); }
void f3(double x) { prt("f3(double)"); }
void f4(int x) { prt("f4(int)"); }
void f4(long x) { prt("f4(long)"); }
void f4(float x) { prt("f4(float)"); }
void f4(double x) { prt("f4(double)"); }
void f5(long x) { prt("f5(long)"); }
void f5(float x) { prt("f5(float)"); }
void f5(double x) { prt("f5(double)"); }
void f6(float x) { prt("f6(float)"); }
void f6(double x) { prt("f6(double)"); }
void f7(double x) { prt("f7(double)"); }
void testConstVal() {
prt("Testing with 5");
f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);
}
void testChar() {
char x = 'x';
prt("char argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testByte() {
byte x = 0;
prt("byte argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testShort() {
short x = 0;
prt("short argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testInt() {
int x = 0;
prt("int argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testLong() {
long x = 0;
prt("long argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testFloat() {
float x = 0;
prt("float argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testDouble() {
double x = 0;
prt("double argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
public static void main(String[] args) {
HahuTest p = new HahuTest();
p.testConstVal();
p.testChar();
p.testByte();
p.testShort();
p.testInt();
p.testLong();
p.testFloat();
p.testDouble();
}
} ///:~
Testing with 5
f1(int)
f2(int)
f3(int)
f4(int)
f5(long)
f6(float)
f7(double)
char argument:
f1(char)
f2(int)
f3(int)
f4(int)
f5(long)
f6(float)
f7(double)
byte argument:
f1(byte)
f2(byte)
f3(short)
f4(int)
f5(long)
f6(float)
f7(double)
short argument:
f1(short)
f2(short)
f3(short)
f4(int)
f5(long)
f6(float)
f7(double)
int argument:
f1(int)
f2(int)
f3(int)
f4(int)
f5(long)
f6(float)
f7(double)
long argument:
f1(long)
f2(long)
f3(long)
f4(long)
f5(long)
f6(float)
f7(double)
float argument:
f1(float)
f2(float)
f3(float)
f4(float)
f5(float)
f6(float)
f7(double)
double argument:
f1(double)
f2(double)
f3(double)
f4(double)
f5(double)
f6(double)
f7(double)
通过上面例子知道,整数常数(5)会被转换成int类型,但是如果参数类型为char,byte或者short,传入整数常数能正常工作吗?答案是不能,看下面例子
class VarTest {
static void prt(String s) {
System.out.println(s);
}
void f1(char x) { prt("f1(char)"); }
void f2(byte x) { prt("f2(byte)"); }
void f3(short x) { prt("f3(short)"); }
void testCons() {
prt("Constant Argument:");
<span style="color:#ff0000;">f1(5); //Cannot compile as '5' is an int
f2(5); //Cannot compile as '5' is an int
f3(5); //Cannot compile as '5' is an int</span>
}
}
char也会自动转换成int类型,同样如果method参数为type或者short,传入char类型能正常工作吗?
class CharTest {
static void prt(String s) {
System.out.println(s);
}
void f2(byte x) { prt("f2(byte)"); }
void f3(short x) { prt("f3(short)"); }
void testCons() {
prt("Char Argument:");
char c = '5';
<span style="color:#ff0000;">f2(c); //Cannot compile as c is char type(or will be converted to int)
f3(c); //Cannot compile as c is char type(or will be converted to int)</span>
}
}