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

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

      • <bdo id='FPVep'></bdo><ul id='FPVep'></ul>

      连接字符以形成字符串会产生不同的结果

      时间:2023-07-28
        <bdo id='dJScG'></bdo><ul id='dJScG'></ul>
      • <tfoot id='dJScG'></tfoot>
          <tbody id='dJScG'></tbody>

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

          <legend id='dJScG'><style id='dJScG'><dir id='dJScG'><q id='dJScG'></q></dir></style></legend>

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

              1. 本文介绍了连接字符以形成字符串会产生不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                为什么,当我使用下面的操作对字符求和时,它返回的是数字而不是字符?它不应该给出相同的结果吗?

                Why, when I use the operation below to sum chars, does it return numbers instead of chars? Shouldn't it give the same result?

                ret += ... ; // returns numbers
                
                ret = ret + ...; // returns chars
                

                下面的代码复制了字符:

                The code below duplicates the chars:

                doubleChar("The") → "THhee"

                doubleChar("The") → "TThhee"

                public String doubleChar(String str) {
                
                    String ret = "";
                    for(int i = 0; i < str.length(); i++) {
                        ret = ret + str.charAt(i) + str.charAt(i); // it concatenates the letters correctly
                        //ret += str.charAt(i) + str.charAt(i); // it concatenates numbers
                    }
                    return ret;
                }
                

                推荐答案

                下面表达式的结果

                ret + str.charAt(i) + str.charAt(i); 
                

                是字符串连接的结果.Java 语言规范声明

                is the result of String concatenation. The Java language specification states

                字符串连接的结果是一个String对象的引用那是两个操作数字符串的连接.那些角色左边的操作数在右边的字符之前新创建的字符串中的操作数.

                The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.

                结果

                str.charAt(i) + str.charAt(i); 
                

                是加法运算符应用于两种数字类型的结果.Java 语言规范声明

                is the result of the additive operator applied to two numeric types. The Java language specification states

                二元 + 运算符在应用于两个操作数时执行加法数字类型,产生操作数的总和.[...]数字操作数上的加法表达式的类型是 promoted其操作数的类型.

                The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands. [...] The type of an additive expression on numeric operands is the promoted type of its operands.

                在这种情况下

                str.charAt(i) + str.charAt(i); 
                

                变成一个 int 保存两个 char 值的总和.然后将其连接到 ret.

                becomes an int holding the sum of the two char values. That is then concatenated to ret.

                您可能还想了解复合赋值表达式 +=

                You might also want to know this about the compound assignment expression +=

                E1 op= E2 形式的复合赋值表达式是等价的到E1 = (T) ((E1) op (E2)),其中TE1的类型,除了E1只评估一次.

                A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

                换句话说

                ret += str.charAt(i) + str.charAt(i);
                

                等价于

                ret = (String) ((ret) + (str.charAt(i) + str.charAt(i)));
                                      |                ^ integer addition
                                      |
                                      ^ string concatenation
                

                这篇关于连接字符以形成字符串会产生不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:“char"和“char"之间的区别和“字符串&q 下一篇:java - 如何在java中将String转换/解析为char?

                相关文章

                最新文章

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

                3. <legend id='m8PUo'><style id='m8PUo'><dir id='m8PUo'><q id='m8PUo'></q></dir></style></legend>