Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java String vs StringBuffer返回值_Java_String_Stringbuffer - Fatal编程技术网

Java String vs StringBuffer返回值

Java String vs StringBuffer返回值,java,string,stringbuffer,Java,String,Stringbuffer,请您解释一下为什么在以下代码中String和StringBuffer被区别对待,以及在StringBuffer中追加值而不是在String中追加值的原因 public class MyClass { public static void main(String args[]) { String str = new String("String Rohan "); StringBuffer strBfr = new StringBuff

请您解释一下为什么在以下代码中
String
StringBuffer
被区别对待,以及在
StringBuffer
中追加值而不是在String中追加值的原因

public class MyClass {
    
    public static void main(String args[]) {
        String str = new String("String Rohan ");
    StringBuffer strBfr = new StringBuffer("String Buffer Rohan "); 
        
        strUpdate(str);
        strBfrUpdate(strBfr);
        
        System.out.println(str);
        System.out.println(strBfr);
        
    }
    
    private static void strBfrUpdate(StringBuffer strBfr){
        strBfr.append("Kushwaha");
    }
    
    private static void  strUpdate(String str){
        str += "Kushwaha";
    }
}
结果如下:

弦罗汉

字符串缓冲区Rohan Kushwaha


在strUpdate方法中,保留对名为str的字符串的引用:

private static void  strUpdate(String str){
    str += "Kushwaha";
}
当你写str+=“…”;这意味着:

    str = new String(str + "...");
此时str引用新字符串,但仅在strUpdate方法中引用。在main方法中,它不会“返回”到main。

String str = new String("String Rohan ");
str是指向javajvm堆内存中某个位置的引用(指针)。让我们称这个地方为

str->A

private static void  strUpdate(String str){
    //str here is still pointing to A, but technically this parameter
    //is a copy of the reference of the str pointer in the main-method
    //pointing to the same place in the heap
    //str->A
    str += "Kushwaha";
    // this line is short for str = str + "Kushwaha"
    //what happens here that you create a new String-Object in the heap (B)
    //and assigne the new value "String Rohan Kushwaha" to the new pointer
    //if you print it out now (within this method) you get what you would 
    //expect
    System.out.println(str);
    //will result in "String Rohan Kushwaha"
}
但是main方法的str指针仍然指向堆中仍然只有“String Rohan”的位置。因此,一旦离开方法strUpdate(…)的作用域,复制的指针(被重新分配到位置B)将被删除

希望这有点帮助

我想这是一些有助于你理解的信息


为了清楚地看到区别,有关识别码,请参见

输出(您的将不同):


另请看@Rainer在修改现有字符串时提到的内容。将在字符串池中创建一个新的字符串实例,字符串引用变量将指向新的文本。对旧文本的引用将被删除

在您的例子中,为字符串创建了两个文本。 一种是主方法,另一种是strUpdate方法。 main方法中的引用变量str指向“Rohan”(实际上是在输出中打印的) 虽然strUpdate方法中的引用变量str指向“Rohan Kushwaha”(如果你在这个方法中打印,你会得到“Rohan Kushwaha”)

但当我们使用StringBuffer作为方法的参数时,实际上是在发送对象引用。当我们将对象作为引用传递时,调用方法也可以看到修改。因此,我们将“Rohan Kushwaha”视为字符串
buffer

正如Shriram在他的评论中所说:字符串在Java中是不可变的,一旦创建,它们就可以不改变。这个问题以前被问过很多次(,例如)。

字符串是不可变的(一旦创建就不能更改)对象。Java中的每个不可变对象都是线程安全的,这意味着字符串也是线程安全的。字符串不能由两个线程同时使用

比如说,

String  demo= " hello Rohan " ;
// The above object is stored in constant string pool and its value can not be modified.

demo="Bye Rohan" ;     //new "Bye Rohan" string is created in constant pool and referenced by the demo variable            
 // "hello Rohan " string still exists in string constant pool and its value is not overrided but we lost reference to the  "hello Roahn" string  
另一方面,

StringBuffer是可变的,这意味着可以更改对象的值。通过StringBuffer创建的对象存储在堆中。StringBuffer具有与StringBuilder相同的方法,但StringBuffer中的每个方法都是同步的,即StringBuffer是线程安全的

为了帮助您更清楚地理解,下面的链接有一个很好的例子 这表明HasCode在字符串中存在差异,但对于字符串缓冲区,HashCode是相同的。 在你的例子中,因为字符串是不可变的

strUpdate()方法的连接值不会添加到主字符串中。因此它保持不变。

希望这能回答您的问题。

因为String是不可变的,stringbuffer是可变的。为什么我得到-1?我写的和得到+1的答案有什么区别?
--- String ---
Before update: 390033781
Parameter: 390033781
Modified (a new String): 1177666623
After update: 390033781
--- StringBuffer ---
Before String Buffer update: 1833642009
StringBuffer parameter: 1833642009
StringBuffer object modification: 1833642009
After String Buffer update: 1833642009
--- Final output ---
String Rohan 
String Buffer Rohan Kushwaha
String  demo= " hello Rohan " ;
// The above object is stored in constant string pool and its value can not be modified.

demo="Bye Rohan" ;     //new "Bye Rohan" string is created in constant pool and referenced by the demo variable            
 // "hello Rohan " string still exists in string constant pool and its value is not overrided but we lost reference to the  "hello Roahn" string