• <i id='gYMDO'><tr id='gYMDO'><dt id='gYMDO'><q id='gYMDO'><span id='gYMDO'><b id='gYMDO'><form id='gYMDO'><ins id='gYMDO'></ins><ul id='gYMDO'></ul><sub id='gYMDO'></sub></form><legend id='gYMDO'></legend><bdo id='gYMDO'><pre id='gYMDO'><center id='gYMDO'></center></pre></bdo></b><th id='gYMDO'></th></span></q></dt></tr></i><div id='gYMDO'><tfoot id='gYMDO'></tfoot><dl id='gYMDO'><fieldset id='gYMDO'></fieldset></dl></div>
    <tfoot id='gYMDO'></tfoot>

      <bdo id='gYMDO'></bdo><ul id='gYMDO'></ul>
    <legend id='gYMDO'><style id='gYMDO'><dir id='gYMDO'><q id='gYMDO'></q></dir></style></legend>
  • <small id='gYMDO'></small><noframes id='gYMDO'>

        Java:带有char数组的println给出乱码

        时间:2023-07-28

                <bdo id='5lHhO'></bdo><ul id='5lHhO'></ul>
                  <legend id='5lHhO'><style id='5lHhO'><dir id='5lHhO'><q id='5lHhO'></q></dir></style></legend>
                    <tbody id='5lHhO'></tbody>
                1. <tfoot id='5lHhO'></tfoot>
                2. <small id='5lHhO'></small><noframes id='5lHhO'>

                  <i id='5lHhO'><tr id='5lHhO'><dt id='5lHhO'><q id='5lHhO'><span id='5lHhO'><b id='5lHhO'><form id='5lHhO'><ins id='5lHhO'></ins><ul id='5lHhO'></ul><sub id='5lHhO'></sub></form><legend id='5lHhO'></legend><bdo id='5lHhO'><pre id='5lHhO'><center id='5lHhO'></center></pre></bdo></b><th id='5lHhO'></th></span></q></dt></tr></i><div id='5lHhO'><tfoot id='5lHhO'></tfoot><dl id='5lHhO'><fieldset id='5lHhO'></fieldset></dl></div>
                  本文介绍了Java:带有char数组的println给出乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这就是问题所在.这段代码:

                  String a = "0000";System.out.println(a);char[] b = a.toCharArray();System.out.println(b);

                  返回

                  <上一页>00000000


                  但是这段代码:

                  String a = "0000";System.out.println("字符串 a:" + a);char[] b = a.toCharArray();System.out.println("char[] b:" + b);

                  返回

                  <上一页>字符串 a:0000字符 [] b: [C@56e5b723


                  世界上到底发生了什么?似乎应该有一个足够简单的解决方案,但我似乎无法弄清楚.

                  解决方案

                  当你说

                  System.out.println(b);

                  它导致调用 print(char[] s) 然后 println()

                  print(char[] s) 的 JavaDoc 说:

                  <块引用>

                  打印一个字符数组.字符转换为字节根据平台的默认字符编码,而这些字节完全按照 write(int) 方法的方式写入.

                  所以它会逐字节打印出来.

                  当你说

                  System.out.println("char[] b: " + b);

                  这会导致调用 print(String),因此您实际上正在做的是将 String 附加到 Object它在 Object 上调用 toString() - 这与默认情况下的所有 Object 以及在 Array 的情况下一样,打印引用的值(内存地址).

                  你可以这样做:

                  System.out.println("char[] b: " + new String(b));

                  请注意,这是错误的",因为您不关心编码并且使用系统默认值.尽早了解编码.

                  Here's the problem. This code:

                  String a = "0000";
                   System.out.println(a);
                  char[] b = a.toCharArray();
                   System.out.println(b);
                  

                  returns

                  0000
                  0000
                  


                  But this code:

                  String a = "0000";
                   System.out.println("String a: " + a);
                  char[] b = a.toCharArray();
                   System.out.println("char[] b: " + b);
                  

                  returns

                  String a: 0000
                  char[] b: [C@56e5b723
                  


                  What in the world is going on? Seems there should be a simple enough solution, but I can't seem to figure it out.

                  解决方案

                  When you say

                  System.out.println(b);
                  

                  It results in a call to print(char[] s) then println()

                  The JavaDoc for print(char[] s) says:

                  Print 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 write(int) method.

                  So it performs a byte-by-byte print out.

                  When you say

                  System.out.println("char[] b: " + b);
                  

                  It results in a call to print(String), and so what you're actually doing is appending to a String an Object which invokes toString() on the Object -- this, as with all Object by default, and in the case of an Array, prints the value of the reference (the memory address).

                  You could do:

                  System.out.println("char[] b: " + new String(b));
                  

                  Note that this is "wrong" in the sense that you're not paying any mind to encoding and are using the system default. Learn about encoding sooner rather than later.

                  这篇关于Java:带有char数组的println给出乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在Java中将字符从字母字符转换为十六进制数 下一篇:替换字符串中的字符的有效方法(java)?

                  相关文章

                  最新文章

                3. <legend id='xzN73'><style id='xzN73'><dir id='xzN73'><q id='xzN73'></q></dir></style></legend>
                  <i id='xzN73'><tr id='xzN73'><dt id='xzN73'><q id='xzN73'><span id='xzN73'><b id='xzN73'><form id='xzN73'><ins id='xzN73'></ins><ul id='xzN73'></ul><sub id='xzN73'></sub></form><legend id='xzN73'></legend><bdo id='xzN73'><pre id='xzN73'><center id='xzN73'></center></pre></bdo></b><th id='xzN73'></th></span></q></dt></tr></i><div id='xzN73'><tfoot id='xzN73'></tfoot><dl id='xzN73'><fieldset id='xzN73'></fieldset></dl></div>

                    <small id='xzN73'></small><noframes id='xzN73'>

                      <bdo id='xzN73'></bdo><ul id='xzN73'></ul>
                    <tfoot id='xzN73'></tfoot>