A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zhangyuren 中级黑马   /  2016-10-27 23:30  /  4135 人查看  /  18 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

问题:
        1,为什么char类型不能输出
        2,char类型怎么修改后,会输出,输出值是什么样?

代码描述:
class Demo_TypeArray {
        public static void main(String[] args) {
                byte[] arr1 = new byte[2];
                short[] arr2 = new short[2];
                int[] arr3 = new int[2];
                long[] arr4 = new long[2];
                float[] arr5 = new float[2];
                double[] arr6 = new double[2];
                boolean[] arr7 = new boolean[2];
                char[] arr8 = new char[2];
                String[] arr9 = new String[2];
               
                //下面的显示语句有测试的注释,知识格式,内容不真实,后面都有8个16进制地址值用e代表
                System.out.println(arr1);        //[B@eeeeeeee
                System.out.println(arr2);        //[S@eeeeeeee
                System.out.println(arr3);        //[I@eeeeeeee
                System.out.println(arr4);        //[J@eeeeeeee
                System.out.println(arr5);        //[F@eeeeeeee
                System.out.println(arr6);        //[D@eeeeeeee
                System.out.println(arr7);        //[Z@eeeeeeee
                System.out.println(char[] arr8);        //无法显示
                System.out.println(arr9);        //[Ljava.lang.String;@eeeeeeee               
        }
}

自我分析:
查了资料,问了人,但是得到的结论是:因为底层对char数组重写了,所以输出不合适.

我想说:
1,这个能不能带着我看看到底是怎样实现的,到底内部是怎样处理的.
2,既然不合适,为什么升级了很多代,这个不进行问题处理呢?

18 个回复

倒序浏览
大神,我学到现在都没考虑那么多,每天内容太多了
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
默默默默 发表于 2016-10-28 00:07
大神,我学到现在都没考虑那么多,每天内容太多了

我有好多内容要发,但是不给我黑马币了.

等以后每天发3个

得技术分太难.


你是怎么得到那么多黑马币的,教教我.
回复 使用道具 举报
默默默默 发表于 2016-10-28 00:07
大神,我学到现在都没考虑那么多,每天内容太多了

是这样的,因为我每天都有总结很多小知识和代码尝试以及发现的问题.
就写了帖子.

但是发现,发主题帖,每天只有四个会给予黑马币,多了没有,跟那个"滔哥"的技术分获得说明有出入,不是5个
写有质量的回复得到黑马币,也只有3个,不是5个.



为此不开心.
回复 使用道具 举报
因为输出流System.out是PrintStream对象,PrintStream有多个重载的println方法,其中一个就是public void println(char[] x),直接打印字符数组的话,不像int[]等其他数组,它会直接调用这个方法来打印,因而可以打印出数组内容,而不是地址。
回复 使用道具 举报
芳燚乐乐 发表于 2016-10-28 14:20
因为输出流System.out是PrintStream对象,PrintStream有多个重载的println方法,其中一个就是public void p ...

那如何获取char数组的内存地址呢?

因为我这篇文章的尝试代码的目的就是想获取char数组的地址.
回复 使用道具 举报
不错张知识了
回复 使用道具 举报

你的黑马币好多,你真棒.

能不能教教我怎么的黑马币?
回复 使用道具 举报
羡慕你们这些学习好的人
回复 使用道具 举报
我也存在同样的疑惑!!!
回复 使用道具 举报
char数组重写toString了
回复 使用道具 举报
zhangyuren 发表于 2016-10-28 22:20
那如何获取char数组的内存地址呢?

因为我这篇文章的尝试代码的目的就是想获取char数组的地址. ...

数组.toString()的方法就可以啦
回复 使用道具 举报
char数组除了保存数字,还要有对应码表,是因为这样吧?
来自宇宙超级黑马专属苹果客户端来自宇宙超级黑马专属苹果客户端
回复 使用道具 举报
yeshusheng 发表于 2016-10-28 23:51
羡慕你们这些学习好的人

我好羡慕你们这些技术分高的人.

我想提技术分,好难呀!
回复 使用道具 举报
芳燚乐乐 发表于 2016-10-29 10:16
数组.toString()的方法就可以啦

我这就试试.
回复 使用道具 举报
芳燚乐乐 发表于 2016-10-29 10:16
数组.toString()的方法就可以啦

调用toString方法,那不就是将它的内容当参,传给了toString,可以说,就是另外开辟了一个堆空间,存储字符串,那就是第二个内存空间了.

可以接受,但是觉得不是最初期待的.

跟C还是不一样.
回复 使用道具 举报
题主,我赋值后就能打印出来了哦。
public class Test
{
        public static void main(String [] args){
        char[] arr8 = new char[2];
        arr8[0]='1';
        System.out.println(arr8);
        }
}

public class Test
{
        public static void main(String [] args){
        char[] arr8 = new char[2];
        arr8[0]='1';
        System.out.println(arr8);
        }
}

D:\黑马\day10\day10>java Test
1

D:\黑马\day10\day10>
下面是查JDK后的知println(char [] array)的JAVA大致调用了的方法。
希望对你有用罗。
/**
     * Prints an array of characters and then terminate the line.  This method
     * behaves as though it invokes <code>{@link #print(char[])}</code> and
     * then <code>{@link #println()}</code>.
     *
     * @param x  an array of chars to print.
     */
   

public void println(char x[])

{
        synchronized (this)
         {
            print(x);
         
                  newLine();
        
        }
   
}

/**
     * Prints an array of characters.  The characters are converted into bytes
     * according to the platform's default character encoding, and these bytes
     * are written in exactly the manner of the
     * <code>{@link #write(int)}</code> method.
     *
     * @param      s   The array of chars to be printed
     *
     * @throws  NullPointerException  If <code>s</code> is <code>null</code>
     */

  public void print(char s[]) {
        write(s);
    }

public void write(byte[] b)
           throws IOException将 b.length 个字节写入此输出流。
FilterOutputStream 的 write 方法将 b、0 和 b.length 作为三个参数来调用 write 方法。

注意,此方法不调用其底层流的只带有单个参数 b 的 write 方法。
覆盖:
类 OutputStream 中的 write
参数:
b - 要写入的数据。
抛出:
IOException - 如果发生 I/O 错误。
回复 使用道具 举报
cobblerime 发表于 2016-10-30 00:59
题主,我赋值后就能打印出来了哦。
public class Test
{

谢谢,你真棒!

我一会会认真看看源码的.

对了,你赋值后的,打印的是内容,不是地址值!

交个朋友吧!
回复 使用道具 举报
学习了各位!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马