ch2 = 'E';
ch1 = 0;
System.out.print("the value of the ch1 and ch2 is:");
System.out.println(ch1 + " " + ch2 + " ends");
两个线被印刷 但如果CH1 = 0,那么,第二打印语句不执行。分配给CH1 0以外的任何值分配0到字符在Java
ch2 = 'E';
ch1 = 0;
System.out.print("the value of the ch1 and ch2 is:");
System.out.println(ch1 + " " + ch2 + " ends");
两个线被印刷 但如果CH1 = 0,那么,第二打印语句不执行。分配给CH1 0以外的任何值分配0到字符在Java
的int
0
相当于一个ASCII NUL
(这恰好是在许多语言中String
终止字符,但Java是不是其中之一)。这个问题(在Eclipse中也是可重现的)在于控制台。
char ch1 = '\0';
System.out.printf("the value of the char is: %c ends%n", ch1);
您正在为char指定0,它应该用单引号引起来。试试这个:
ch2 = 'E';
ch1 = '0';
System.out.print("the value of the ch1 and ch2 is:");
System.out.println(ch1 + " " + ch2 + " ends");
希望这会有所帮助。
你可以用UNICODE的范围内的任何int值初始化char! – Hrcule
http://ideone.com/7HNoeI –