1.int和char比较
public static void main(String[] args) {
char x = 'x';
int i = 10;
char y = '2';
System.out.println(true? x: i);
System.out.println(true? 'x': 1167);
System.out.println(true?y:2);
System.out.println((int)x);
}
JVM:
public static void main(String[] args) {
char x = 120;
int i = true;
char y = '2';
System.out.println(x);
System.out.println('x');
System.out.println(y);
System.out.println(x);
}
从结果来看,int和char比较,
当char和常量int比较,输出的是字符型,char和变量int比较时,输出的是ASCII型,也就是说,char和int的变量比较,会直接提升为int型比较
2。char和double比较
public static void main(String[] args) {
char x = 'x';
double i = 10.5;
System.out.println(true? x: i);
System.out.println(true? 'x': 1167.5);
}
JVM
public static void main(String[] args) {
char x = 120;
double i = 10.5;
char y = true;
System.out.println((double)x);
System.out.println(120.0);
}
char和double比较,无论常量变量,都必须先转换为double进行比较
3.char和byte/short比较
public static void main(String[] args) {
char x = 'x';
byte i = 10;
byte j = 10;
System.out.println(true? x: i);
System.out.println(true? j: x);
System.out.println(true? 'x': 1167.5);
}
public static void main(String[] args) {
char x = 120;
byte i = true;
byte j = 10;
System.out.println(x);
System.out.println(j);
System.out.println(120.0);
}
默认byte为true,但是如果结果有byte比较成功,则是本身,应该还是强转为了int进行比较的,
short与byte一致