当前位置: 技术问答>java相关
请问以下程序结果,谢谢
来源: 互联网 发布时间:2014-12-27
本文导语: What happens on trying to compile and run the following code? public class EqualsTest{ public static void main(String args[]){ char A='u0005'; if(A==0x0005L) System.out.println("Equal"); else System.out.println("Not Equal"); } } a) The compiler reports "Inval...
What happens on trying to compile and run the following code?
public class EqualsTest{
public static void main(String args[]){
char A='u0005';
if(A==0x0005L) System.out.println("Equal");
else System.out.println("Not Equal");
}
}
a) The compiler reports "Invalid character in input" in line 3.
b) The program compiles and prints "Not Equal".
c) The program compiles and prints "Equal".
我试过这段程序,结果是 Equal
A是char, 而 0x0005L应该是long型吧。两个比较为什么相等,我用System.out.pringln(A); 没有结果显示,而 System.out.println(0x0005L)结果显示5,为什么他们会相等呢???
谢谢
public class EqualsTest{
public static void main(String args[]){
char A='u0005';
if(A==0x0005L) System.out.println("Equal");
else System.out.println("Not Equal");
}
}
a) The compiler reports "Invalid character in input" in line 3.
b) The program compiles and prints "Not Equal".
c) The program compiles and prints "Equal".
我试过这段程序,结果是 Equal
A是char, 而 0x0005L应该是long型吧。两个比较为什么相等,我用System.out.pringln(A); 没有结果显示,而 System.out.println(0x0005L)结果显示5,为什么他们会相等呢???
谢谢
|
char与long比较时char转换成long(高位补0) 所以相等
println(char aChar)和println(long aLong)执行不同的操作所以输出不同
println(char aChar)打印字符,而'u0005'是不可见字符,所以“没有结果显示“
println(long aLong)打印long的值,所以System.out.println(0x0005L)结果显示5
println(char aChar)和println(long aLong)执行不同的操作所以输出不同
println(char aChar)打印字符,而'u0005'是不可见字符,所以“没有结果显示“
println(long aLong)打印long的值,所以System.out.println(0x0005L)结果显示5