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

Java Puzzlers笔记--puzzle 12: ABC String与char的区别

2007-03-04 10:45 447 查看
public class Abc{
 public static void main(String[] args){
  String letters = "ABC";
  char[] numbers = {"1", "2", "3"};
  System.out.println(letters + " easy as " + numbers );
 }
}

Solution:

               显示(像是):ABC easy as [C@16f0472

               由于char[] numbers 是一个类对象,而在+中又重载了Object取得对象地址的方法。

TID:

              The char[] overloading of println prints all of the characters contained in the array, and the char[] overloading of String.valueOf and StringBuffer.append behave analogously.

              REturns a string consisting of the name of the class of which the object is an instance, the at-sign character '@', and the unsigned hexadecimal representation of the hash code of the object.

Correctly:

               System.out.println(letters + " easy as " + String.valueOf(numbers));

or:

               System.out.print(letters + " easy as ");

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