万圣节。。。 欢乐谷,玩得太累了 人真是超级超级超级多。。。。 忘记更新博客。 说好的每天一更。。 从温暖的被窝爬起来。。。。 希望明天不会感冒吧
偷个懒,找了个以前看tininking的时候测试代码:
package codeTest; public class BasicTypeTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // t.getName() BasicTypeTest t = new BasicTypeTest(); t.testFive(); t.testByte(); t.testDouble(); t.testFloat(); t.testInt(); t.testLong(); t.testShort(); t.testChar(); System.err.println("1222333333333333"); } void f1(int i) { prt("f1 int"); } void f1(char i) { prt("f1 char"); } void f1(float i) { prt("f1 float"); } void f1(double i) { prt("f1 double"); } void f1(byte i) { prt("f1 byte"); } void f1(short i) { prt("f1 short"); } void f1(long i) { prt("f1 long"); } void f2(int i) { prt("f2 int"); } void f2(float i) { prt("f2 float"); } void f2(double i) { prt("f2 double"); } void f2(byte i) { prt("f2 byte"); } void f2(short i) { prt("f2 short"); } void f2(long i) { prt("f2 long"); } void f3(int i) { prt("f3 int"); } void f3(float i) { prt("f3 float"); } void f3(double i) { prt("f3 double"); } void f3(short i) { prt("f3 short"); } void f3(long i) { prt("f3 long"); } void f4(int i) { prt("f4 int"); } void f4(float i) { prt("f4 float"); } void f4(double i) { prt("f4 double"); } void f4(long i) { prt("f4 long"); } void f5(float i) { prt("f5 float"); } void f5(double i) { prt("f5 double"); } void f5(long i) { prt("f5 long"); } void f6(float i) { prt("f6 float"); } void f6(double i) { prt("f6 double"); } void f7(double i) { prt("f7 double"); } static void prt(String s) { System.out.println(s); } void testFive() { prt("Test 5:"); f1(5); f2(5); f3(5); f4(5); f5(5); f6(5); f7(5); } void testByte() { byte x = 0; prt("test byte"); f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); } void testShort() { short x = 0; prt("test short"); f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); } void testChar() { char x = 0; prt("test char"); f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); } void testInt() { int x = 0; prt("test int"); f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); } void testLong() { long x = 0; prt("test long"); f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); } void testFloat() { float x = 0; prt("test float"); f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); } void testDouble() { double x = 0; prt("test double"); f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); } }
运行结果:
Test 5: f1 int f2 int f3 int f4 int f5 long f6 float f7 double test byte f1 byte f2 byte f3 short f4 int f5 long f6 float f7 double test double f1 double f2 double f3 double f4 double f5 double f6 double f7 double test float f1 float f2 float f3 float f4 float f5 float f6 float f7 double test int f1 int f2 int f3 int f4 int f5 long f6 float f7 double test long f1 long f2 long f3 long f4 long f5 long f6 float f7 double test short f1 short f2 short f3 short f4 int f5 long f6 float f7 double test char f1 char f2 int f3 int f4 int f5 long f6 float f7 double 1222333333333333
这说明什么? 说明基础类型是可以自己向上“塑形”的。。
就是说 如果我们需要的是double类型的数据,你往方法里面传其他的任何类型的基础数据都是可以的。如下边这段代码是没问题的
public static void main(String[] args) { // TODO Auto-generated method stub int i = 2; char a = 'a'; f(i); f(a); byte b = 0; f(b); float f = 9; f(f); } public static void f(double b){ System.out.println(b); }
因为double在基础类型里面是“最大”的类型了。而如果上面这段代码改成这样,编译器就会报错
public static void main(String[] args) { // TODO Auto-generated method stub int i = 2; char a = 'a'; f(i); f(a); byte b = 0; f(b); float f = 9; f(f); } public static void f(int b){ System.out.println(b); }
报错The method f(int) in the type BasicTypeTest2 is not applicable for the arguments (float) 只能通过主动的显式修改参数类型:f((int)f);
通过对第一段长长的代码分析,得知java中基础类型的顺序为:
byte<short<int<long<float<double
当较小的类型被当作参数传递的时候,可以自动转换为方法需要的较大的类型。
char有些不同,比char小的byte等不能转换为char,但是char可以转换为比他大的int,long等类型。thinking对此的解释是,如果jvm在调用参数的时候发现他需要char ,而调用的不是精准的char类型,只是一个数值,那么不管他是否比char小,都被当成int类型来处理。
1点20......睡觉。。。