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

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

          <bdo id='xljLs'></bdo><ul id='xljLs'></ul>

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

      1. <tfoot id='xljLs'></tfoot>

        为什么可以在 Java 中连接 Char 和 String?

        时间:2023-07-26

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

              <tbody id='oOLf8'></tbody>

              <tfoot id='oOLf8'></tfoot>
            1. <small id='oOLf8'></small><noframes id='oOLf8'>

                <bdo id='oOLf8'></bdo><ul id='oOLf8'></ul>
                • 本文介绍了为什么可以在 Java 中连接 Char 和 String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这是我尝试过的;

                  public String frontBack(String str) {
                        char begin = str.charAt(0);
                        char end = str.charAt(str.length()-1);
                        return begin+str.substring(1,str.length()-1)+end;
                  }
                  

                  我认为它会失败,因为 begin 和 end 是字符,但事实并非如此.这怎么可能?谁能给我解释一下?谢谢!

                  I've thought it would fail since begin and end are chars but it doesn't. How is that possible? Can anyone explain me please? Thanks!

                  推荐答案

                  Java 的 '+' 运算符也可用作连接运算符.它可以连接原语和对象,并返回一个字符串作为它的结果.

                  Java's '+' operator also serves as a concatenation operator. It can concatenate primitives and objects and would return you a string which is its result.

                  以下解释假设您熟悉 Java 的包装类.如果您不熟悉它们,请阅读.

                  The following explanation assumes that you are familiar with Java's wrapper classes. In case you are not familiar with them, please give it a read.

                  Java 的+"运算符将语句中使用的所有原始数据类型转换为其等效的 Wrapper 类,并在这些实例上调用 toString() 方法并使用该结果,即表达式中的字符串.

                  Java's '+' operator converts all the primitive data types used in the statement to their equivalent Wrapper classes and invokes toString() method on those instances and uses that result, which is a string in the expression.

                  例如:在 Java 中,像 System.out.println( 3 + " Four " + 'C' ); 这样的语句最终会创建一个内容为 "3 Four C".

                  Ex: In Java, a statement like System.out.println( 3 + " Four " + 'C' ); ends up creating a String with the content "3 Four C".

                  在上面的语句中,3 是一个原始的 int 变量.四"是一个字符串对象,C"是一个原始字符变量.

                  In the above statement, 3 is a primitive int variable. " Four " is a String object and 'C' is a primitive char variable.

                  在 '+' 连接操作期间,3 被转换为其对应的 Wrapper 类 -> Integer.然后对其调用 toString() 方法.输出为字符串 3,即 "3"四"已经是一个字符串,不需要进一步处理.'C' 被转换为字符包装类,并且 toString() 方法返回字符串C".

                  During '+' concat operation, 3 gets converted to its corresponding Wrapper class -> Integer. And then toString() method is called on it. Output is String 3 i.e., "3" " Four " is already a String and needs no further processing. 'C' gets converted to Character wrapper class and toString() method results in returning the String "C".

                  所以最后,将这三个相加,得到3 Four C".

                  So finally, these three are added so that you get "3 Four C".

                  总结一下:

                  1. 如果在 '+' 运算符中使用原语,则将其转换为 Wrapper 类,然后对其调用 toString() 方法并将结果用于追加.
                  2. 如果使用 String 以外的对象,则会调用其 toString() 方法,并将其结果用于追加.
                  3. 如果调用了一个字符串,那么没有什么可做的,字符串本身就被用于追加.

                  这篇关于为什么可以在 Java 中连接 Char 和 String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  <legend id='aDWaK'><style id='aDWaK'><dir id='aDWaK'><q id='aDWaK'></q></dir></style></legend>
                    <tbody id='aDWaK'></tbody>

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