ybaidukuai 发表于 2017-5-27 10:25:17

关于字符串连接操作符(三)(源自IBM蓝色之路一道笔试题目)

再看下下面的例子

public class TestChar1 {   
public static void main(String[] args) {
// int类型的常量可以赋值给任意类型的变量
byte b = (int) 1;   //OK
short s = (int) 1;//OK
int i = (int) 1;    //OK
long l = (int) 1;   //OK
float f = (int) 1;//OK
double d = (int) 1; //OK
char c = (int) 1;   //OK
}   
}
public class TestChar2 {   
public static void main(String[] args) {
// int类型的变量只能赋值给高类型的变量,不能赋值给低类型的变量(会损失精度)      
int a = 1;
char c = (int) a;
// Type mismatch: cannot convert from int to char      
byte b = (int) a;
// Type mismatch: cannot convert from int to char
short s = (int) a;   
// Type mismatch: cannot convert from int to char
int i = (int) a;   //OK
long l = (int) a;//OK
float f = (int) a; //OK
double d = (int) a;//OK   
}   
}
public class TestChar3 {   
public static void main(String [] args) {   
int i = 1;   
// Type mismatch: cannot convert from int to char
char c = i;   
System.out.println(c);   
}   
}
public class TestChar4 {   
public static void main(String [] args) {   
char c = (int)1; //Ok, 等价于: char c='\001';
char d = '\001'; //OK
System.out.println(c==d); //output: true    }   
}
页: [1]
查看完整版本: 关于字符串连接操作符(三)(源自IBM蓝色之路一道笔试题目)