| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 514 人关注过本帖
标题:Puzzlers with Character
只看楼主 加入收藏
桂圆
Rank: 1
等 级:新手上路
威 望:1
帖 子:101
专家分:0
注 册:2006-5-15
收藏
 问题点数:0 回复次数:1 
Puzzlers with Character
What does the following program print?


public class LastLaugh {

public static void main(String args[]) {

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

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

}

}




Solution 11: The Last Laugh
If you are like most people, you thought that the program would print HaHa. It looks as though it concatenates H to a in two ways, but looks can be deceiving. If you ran the program, you found that it prints Ha169. Now why would it do a thing like that?

As expected, the first call to System.out.print prints Ha: Its argument is the expression "H" + "a", which performs the obvious string concatenation. The second call to System.out.print is another story. Its argument is the expression 'H' + 'a'. The problem is that 'H' and 'a' are char literals. Because neither operand is of type String, the + operator performs addition rather than string concatenation.

The compiler evaluates the constant expression 'H' + 'a' by promoting each of the char-valued operands ('H' and 'a') to int values through a process known as widening primitive conversion [JLS 5.1.2, 5.6.2]. Widening primitive conversion of a char to an int zero extends the 16-bit char value to fill the 32-bit int. In the case of 'H', the char value is 72 and in the case of 'a', it is 97, so the expression 'H' + 'a' is equivalent to the int constant 72 + 97, or 169.

From a linguistic standpoint, the resemblance between char values and strings is illusory. As far as the language is concerned, a char is an unsigned 16-bit primitive integer—nothing more. Not so for the libraries. They contain many methods that take char arguments and treat them as Unicode characters.

So how do you concatenate characters? You could use the libraries. For example, you could use a string buffer:


StringBuffer sb = new StringBuffer();

sb.append('H');

sb.append('a');

System.out.println(sb);




This works, but it's ugly. There are ways to avoid the verbosity of this approach. You can force the + operator to perform string concatenation rather than addition by ensuring that at least one of its operands is a string. The common idiom is to begin a sequence of concatenations with the empty string (""), as follows:

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


This idiom ensures that subexpressions are converted to strings. Although useful it is a bit ungainly and can lead to some confusion itself.

Can you guess what the following statement prints? If you aren't sure, try it:

System.out.println("2 + 2 = " + 2+2);


As of release 5.0, you also have the option of using the printf facility:

System.out.printf("%c%c", 'H', 'a');


In summary, use the string concatenation operator with care. The + operator performs string concatenation if and only if at least one of its operands is of type String; otherwise, it performs addition. If none of the values to be concatenated are strings, you have several choices: prepend the empty string; convert the first value to a string explicitly, using String.valueOf; use a string buffer; or if you are using release 5.0, use the printf facility.

This puzzle also contains a lesson for language designers. Operator overloading, even to the limited extent that it is supported in Java, can be confusing. It may have been a mistake to overload the + operator for string concatenation.

值得好好一看 E文好的朋友看看吧

猜猜看代码输出什么
然后再上机试下 呵呵
真的很不错
搜索更多相关主题的帖子: Character Puzzlers 
2006-06-25 13:37
水影月圆
Rank: 4
等 级:贵宾
威 望:11
帖 子:738
专家分:0
注 册:2005-8-2
收藏
得分:0 

输出结果为:Ha169Ha
public class LastLaugh
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer();
sb.append('H');
sb.append('a');
// System.out.println(sb); //1
System.out.print("H" + "a");
// System.out.println(sb); //2
System.out.print('H' + 'a');
System.out.println(sb); //3
}

}
System.out.print("H" + "a"); 和 System.out.print('H' + 'a'); 是不同的 大家要看清楚啊 一个是双引 一个是单引
单引的是输出H和a的ASCII码值的 所以如果1和2给注释掉的话 那输入的结果是 Ha+他们的ASCII码值+sb的值

很简单的 你们该能看的懂吧

[此贴子已经被作者于2006-6-25 13:55:30编辑过]


子非鱼,安知鱼之江湖?子非我,安知我之功夫 http://20681.
2006-06-25 13:41
快速回复:Puzzlers with Character
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025689 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved