----------------------------------- hamid1455 Wed Nov 16, 2016 3:47 pm immutable object string ----------------------------------- If a String Object is immutable in Java, it means that once it is created it can not be changed right? So if I have made a String like this: String name = "Bob"; and I do this on the next line: name = "Joe"; Does the second line create a new String Object and set its contents to the object name? Because I read that when you concatenate Strings, it actually creates a new String and returns that, because it can't change the original String. ----------------------------------- Insectoid Wed Nov 16, 2016 6:27 pm RE:immutable object string ----------------------------------- I believe you are correct. name is a reference (or a pointer? I forget) to the actual object. The = operator assigns values to primitives only- Strings are objects. In this case, the primitives are pointer (or reference, I dunno, I'm a C guy) and a memory address. You are assigning the value of the String reference (or pointer) to the address of the String object you just created. This is the same reason you cannot use the == comparison operator with Strings- == will compare the addresses, not the actual content of the string. Note that the first string is technically orphaned. Java will take care of this automatically, but other languages might not. ----------------------------------- DemonWasp Wed Nov 16, 2016 8:18 pm RE:immutable object string ----------------------------------- Java's "references" aren't exactly like a C++ pointer or a C++ reference. They're nullable (unlike C++ references), but you can't do arithmetic on them or dereference arbitrary ones (unlike C++ pointers). If you dereference a null whatever-they-are, you get a NullPointerException so I guess we can call them pointers. To illustrate memory for a moment: [code] String name = "Bob"; name ---> "Bob"