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 –