0

I realize that the hashCode of the variable name, is different after the "update", but objectively what makes a String object in fact immutable ?

public static void main(String[] args) {

        String str = "AB";
         System.out.println(str ); // AB

         str = str .replace(str .charAt(0) ,'W');
         System.out.println(str );//WB

 }

EDIT 1 : The hashCode is based on the value of the variable and have no relation with memory adress.

EDIT 2 : I now understand that Strings are references and not Objects in it self. I read back all the answers for this same question and found out good answers in topics like [this] (Immutability of Strings in Java). Thank you whos tried to help me and my excuses for any silly misunderstood.

I also recommend this articles here to who wants better understand how Strings works in Java :

https://www.pushkarrajpujari.com/article/strings-in-java/

and how references works :

https://javaranch.com/campfire/StoryPassBy.jsp

EDIT 3: I cannot DELETE this topic anymore, according with Stackoverflow "You cannot delete this question as others have invested time and effort into answering it." which I agree.

basquiatraphaeu
  • 525
  • 7
  • 19
  • 7
    The string returned from replace is a different string. The variable is not the object. You just overwrite the reference to one string with a reference to another string. – Nathan Hughes Sep 19 '19 at 21:02
  • 7
    You're not mutating the String. You're assigning a **new** String to the same variable. – Jordan Sep 19 '19 at 21:02
  • Variables are always mutable, in the sense that they can be made to reference new objects, even when the objects themselves are immutable. – Dawood ibn Kareem Sep 19 '19 at 21:10
  • Variables are variable (duh). – Johannes Kuhn Sep 19 '19 at 21:26
  • BTW that is not the address of the variable, but the address of the object the variable is pointing to (or the content of the variable (since it is a reference/pointer)) ((haven't checked the documentation, but doubt that is not explained)) – user85421 Sep 23 '19 at 19:32
  • `addressOf(str)` does not give you the address of the variable `str`, but instead the address of the object it is referencing. Keep in mind that `31864662320` == `0x76b482930`. – Progman Sep 23 '19 at 19:39
  • @Progman, thanks for your answer man. I already erase my misguided edit. – basquiatraphaeu Sep 24 '19 at 00:26
  • @Progman, thanks for your answer man. I already erase my misguided edit. – basquiatraphaeu Sep 24 '19 at 01:01
  • @JohannesKuhn Well, there are also *constant variables*, like ones marked `static final`. – MC Emperor Apr 01 '20 at 22:05
  • @Progman Well, it's not a memory address per se. It is implementation specific, and *appears* to be a memory address or a part of it. See [here for more details](https://stackoverflow.com/questions/1961146/memory-address-of-variables-in-java). – MC Emperor Apr 01 '20 at 22:14

1 Answers1

1

If you look at the documentation of replace(), it mentions:

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

Therefore, the replaced String is an entirely new String.

Anoop R Desai
  • 712
  • 5
  • 18