• <legend id='9hQKK'><style id='9hQKK'><dir id='9hQKK'><q id='9hQKK'></q></dir></style></legend>

      <bdo id='9hQKK'></bdo><ul id='9hQKK'></ul>

      1. <small id='9hQKK'></small><noframes id='9hQKK'>

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

        为什么一个 char + 另一个 char = 一个奇怪的数字

        时间:2023-07-26

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

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

                  <tbody id='Ql3dm'></tbody>
              • <tfoot id='Ql3dm'></tfoot>
                <legend id='Ql3dm'><style id='Ql3dm'><dir id='Ql3dm'><q id='Ql3dm'></q></dir></style></legend>
                1. 本文介绍了为什么一个 char + 另一个 char = 一个奇怪的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  代码片段如下:

                  public static void main (String[]arg) 
                  {
                      char ca = 'a' ; 
                      char cb = 'b' ; 
                      System.out.println (ca + cb) ; 
                  }
                  

                  输出是:

                  195
                  

                  为什么会这样?我认为 'a' + 'b' 将是 "ab""12"3.

                  Why is this the case? I would think that 'a' + 'b' would be either "ab" , "12" , or 3.

                  这是怎么回事?

                  推荐答案

                  + 的两个 char 是算术加法,而不是字符串连接.你必须做类似 ""+ ca + cb,或者使用String.valueOfCharacter.toString方法保证+ 是一个String,用于操作符进行字符串连接.

                  + of two char is arithmetic addition, not string concatenation. You have to do something like "" + ca + cb, or use String.valueOf and Character.toString methods to ensure that at least one of the operands of + is a String for the operator to be string concatenation.

                  如果 + 运算符的任一操作数的类型为 String,则该操作为字符串连接.

                  If the type of either operand of a + operator is String, then the operation is string concatenation.

                  否则,+ 运算符的每个操作数的类型必须是可转换为原始数值类型的类型,否则会出现编译时错误.

                  Otherwise, the type of each of the operands of the + operator must be a type that is convertible to a primitive numeric type, or a compile-time error occurs.

                  至于为什么你得到 195,那是因为在 ASCII 中,'a' = 97'b' = 98,以及 97 + 98= 195.

                  As to why you're getting 195, it's because in ASCII, 'a' = 97 and 'b' = 98, and 97 + 98 = 195.

                  这执行基本的 intchar 转换.

                  This performs basic int and char casting.

                   char ch = 'a';
                   int i = (int) ch;   
                   System.out.println(i);   // prints "97"
                   ch = (char) 99;
                   System.out.println(ch);  // prints "c"
                  

                  这忽略了字符编码方案的问题(初学者不应该担心......但是!).

                  This ignores the issue of character encoding schemes (which a beginner should not worry about... yet!).

                  作为注释,Josh Bloch 指出,很遗憾 + 对字符串连接和整数加法都进行了重载(对于字符串连接重载 + 运算符可能是一个错误." -- Java Puzzlers,谜题 11:最后的笑声).通过使用不同的字符串连接标记可以轻松避免很多此类混淆.

                  As a note, Josh Bloch noted that it is rather unfortunate that + is overloaded for both string concatenation and integer addition ("It may have been a mistake to overload the + operator for string concatenation." -- Java Puzzlers, Puzzle 11: The Last Laugh). A lot of this kinds of confusion could've been easily avoided by having a different token for string concatenation.

                  • 是与空字符串连接进行字符串转换真的那么糟糕吗?

                  这篇关于为什么一个 char + 另一个 char = 一个奇怪的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                      <bdo id='KvV8C'></bdo><ul id='KvV8C'></ul>
                      1. <legend id='KvV8C'><style id='KvV8C'><dir id='KvV8C'><q id='KvV8C'></q></dir></style></legend>

                          • <tfoot id='KvV8C'></tfoot>
                              <tbody id='KvV8C'></tbody>
                          • <small id='KvV8C'></small><noframes id='KvV8C'>

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