日期:2014-05-20  浏览次数:20677 次

java中打印返回值的问题
public class 二维数组{
public static void main(String args[]){
int a[][]={{2,1,1},{5},{6,9}};
int b[]={5,4,3};
aaa s=new aaa();
System.out.println(s);
for(int i=0;i<a.length;i++)
for(int j=0;j<a[i].length;j++)
System.out.println("a[" + i +"]"+"["+j+"]"+"="+a[i][j]);

for(int j=0;j<b.length;j++)
System.out.println("b[" + j +"]"+"="+b[j]);
}
}


class aaa{

}
java中是不是自己新建的类,如果没有重写toString函数println出来的是哈希尔码???
其它都是自己本身的值?
还是说只要是引用,打印出来的都是哈希尔码,但数组不是引用吗?怎么打印出来是自己的值

------解决方案--------------------
Java中自己建的类,默认继承java.lang.Object类。其toString()方法输入的结果是“类名@哈希码”。
Java API文档中原文:
引用
The toString method for class Object 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. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

对于某个类的对象,在打印时默认按照以上规则,除非你去重写它的toString()方法。
但对于基本类型,如int,打印时会直接打印它的值。楼主程序中打印的a[i][j],是指打印int数组中第i行第j列的值,它是个int变量,所以直接打印出它的值。
对于楼主说的打印数组的引用,试试就知道了:

int b[] = {1, 2, 3};
System.out.println(b);

本机测试显示:

[I@1e78fc6

------解决方案--------------------
论坛本身就有这个功能啊。这样就行了: