您的位置:首页 > 编程语言 > Java开发

Java Puzzlers笔记--puzzle 11: The last laugh "" 与''的区别

2007-03-04 10:30 507 查看
public class LastLaugh{

         public static void main(String[] args){

                  System.out.println("H" + "a");

                  System.out.println('H' + 'a');

         }



Solution:

                显示:Ha169

                 因为'H' + 'a' 不是连接2个字符,而是把H和a转ASC码,再进行加法运算;

TID:

                The + operator performs string concatenation if and only if at least one of its operands is of type String;

Correctly:

                StringBuffer sb = new StringBuffer();

                 sb.append('H');

                  sb.append('a');

                  System.out.println(sb);

or:

                 System.out.println("" + 'H' + 'a' );

or:

                 System.out.println("%c%c", 'H', 'a'); // in release 5.0 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息