<tfoot id='o4qXe'></tfoot>

  1. <small id='o4qXe'></small><noframes id='o4qXe'>

      <bdo id='o4qXe'></bdo><ul id='o4qXe'></ul>
  2. <legend id='o4qXe'><style id='o4qXe'><dir id='o4qXe'><q id='o4qXe'></q></dir></style></legend>

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

      字符串不能改变.但是int、char可以改变

      时间:2023-07-28
    1. <i id='ZVNwW'><tr id='ZVNwW'><dt id='ZVNwW'><q id='ZVNwW'><span id='ZVNwW'><b id='ZVNwW'><form id='ZVNwW'><ins id='ZVNwW'></ins><ul id='ZVNwW'></ul><sub id='ZVNwW'></sub></form><legend id='ZVNwW'></legend><bdo id='ZVNwW'><pre id='ZVNwW'><center id='ZVNwW'></center></pre></bdo></b><th id='ZVNwW'></th></span></q></dt></tr></i><div id='ZVNwW'><tfoot id='ZVNwW'></tfoot><dl id='ZVNwW'><fieldset id='ZVNwW'></fieldset></dl></div>

      1. <tfoot id='ZVNwW'></tfoot>
            <bdo id='ZVNwW'></bdo><ul id='ZVNwW'></ul>

              <tbody id='ZVNwW'></tbody>

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

          • <legend id='ZVNwW'><style id='ZVNwW'><dir id='ZVNwW'><q id='ZVNwW'></q></dir></style></legend>
              • 本文介绍了字符串不能改变.但是int、char可以改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我读到在 Java 中,String 类型的对象不能改变.但是 int 和 char 变量可以.为什么?可以举个例子吗?

                I've read that in Java an object of type String can't change. But int and char variables can. Why is it? Can you give me an example?

                谢谢.(我是新人-_-)

                Thank you. (I am a newer -_- )

                推荐答案

                正如 bzabhi 所说,字符串在 Java 中是不可变的.这意味着字符串对象永远不会改变.这并不意味着您不能更改字符串变量,只是您不能更改字符串的底层内存表示.例如:

                As bzabhi said, strings are immutable in Java. This means that a string object will never change. This does not mean you can not change string variables, just that you cannot change the underlying memory representation of the string. for an example:

                String str = "Hello";
                str += " World!";
                

                在这些行执行之后,str 将指向内存中的一个新字符串.原始的Hello"字符串仍然存在于内存中,但很可能不会存在太久.假设没有情有可原的情况,任何东西都不会指向原始字符串,所以它将被垃圾收集.

                Following the execution of these lines, str will point to a new string in memory. The original "Hello" string still exists in memory, but most likely it will not be there for long. Assuming that there are no extenuating circumstances, nothing will be pointing at the original string, so it will be garbage collected.

                我想最好的说法是,当示例的第 2 行执行时,内存中的一个新字符串是由原始字符串和添加到其中的字符串的串联创建的.str 变量只是对内存位置的引用,然后更改为指向刚刚创建的新变量.

                I guess the best way to put this would be to say that when line 2 of the example executes, a new string in memory is created from the concatenation of the original string and the string being added to it. The str variable, which is just a reference to a memory location, is then changed to point at the new variable that was just created.

                我对这一点并不是特别了解,但据我了解,这就是所有非原始"值都会发生的情况.任何从 Object 派生的东西都遵循这些规则.原始值,例如 ints、bools、chars、float 和 doubles 允许更改内存中的实际值.所以,从这里:

                I am not particularly knowledgeable on the point, but, as I understand it, this is what happens with all "non-primitive" values. Anything that at some point derives from Object follows these rules. Primitive values, such as ints, bools, chars, floats and doubles allow the actual value in memory to be changed. So, from this:

                int num = 5;
                num += 2;
                

                内存中的实际值发生变化.此代码示例不会创建新对象并更改引用,而是仅更改 num 变量在内存中的值.

                the actual value in memory changes. Rather than creating a new object and changing the reference, this code sample will simply change the value in memory for the num variable.

                至于为什么会这样,这只是Java制造商的设计决定.我相信有人会评论为什么会这样,但我不知道.

                As for why this is true, it is simply a design decision by the makers of Java. I'm sure someone will comment on why this was made, but that isn't something I know.

                这篇关于字符串不能改变.但是int、char可以改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Java - 为什么 char 不应该被隐式转换为字节(和短 下一篇:用java中的char变量计算

                相关文章

                最新文章

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

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

                2. <small id='FWg5L'></small><noframes id='FWg5L'>

                  1. <legend id='FWg5L'><style id='FWg5L'><dir id='FWg5L'><q id='FWg5L'></q></dir></style></legend>