java中打印返回值的问题

CodeInfo_ 2013-03-08 10:57:41
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出来的是哈希尔码???
其它都是自己本身的值?
还是说只要是引用,打印出来的都是哈希尔码,但数组不是引用吗?怎么打印出来是自己的值
...全文
788 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
冒泡彩虹 2013-03-29
  • 打赏
  • 举报
回复
miaowhehe 2013-03-09
  • 打赏
  • 举报
回复
论坛本身就有这个功能啊。这样就行了:
CodeInfo_ 2013-03-09
  • 打赏
  • 举报
回复
引用 1 楼 miaowhehe 的回复:
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 i……
原来啊,谢谢啦,不过你用什么编译器呀?为什么代码左边会自动编号
miaowhehe 2013-03-08
  • 打赏
  • 举报
回复
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

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧