Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. If string is a immutable class then how come it is allowing us to make changes ?

If string is a immutable class then how come it is allowing us to make changes ?

Scheduled Pinned Locked Moved Java
functionalquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shiva kore
    wrote on last edited by
    #1

    Hi all, I am having one small doubt. If string is a immutable class then how come it is allow us change ? ex: String s1="abc"; s1="def"; Sop(s1); it gives op as :- def. does it mean we have made changes on string object ? please tell me. Thanks ************ S G KORE *******************

    L 1 Reply Last reply
    0
    • S shiva kore

      Hi all, I am having one small doubt. If string is a immutable class then how come it is allow us change ? ex: String s1="abc"; s1="def"; Sop(s1); it gives op as :- def. does it mean we have made changes on string object ? please tell me. Thanks ************ S G KORE *******************

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      "abc" and "def" are strings. s1 is not a string, it is a variable holding a reference to a string. at first it refers to "abc", later on it refers to "def". The string "abc" never got modified, it may still be alive (kept alive by another reference, as in the example below), or it is dead and garbage collectible.

      string s1="abc";
      string s2=s1;
      s1="def";
      Sop(s1); // refers to "def"
      Sop(s2); // refers to "abc"

      :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      S 1 Reply Last reply
      0
      • L Luc Pattyn

        "abc" and "def" are strings. s1 is not a string, it is a variable holding a reference to a string. at first it refers to "abc", later on it refers to "def". The string "abc" never got modified, it may still be alive (kept alive by another reference, as in the example below), or it is dead and garbage collectible.

        string s1="abc";
        string s2=s1;
        s1="def";
        Sop(s1); // refers to "def"
        Sop(s2); // refers to "abc"

        :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        S Offline
        S Offline
        shiva kore
        wrote on last edited by
        #3

        Thank you Luc Pattyn. Now I got clear picture . :) ************ S G KORE *******************

        C 1 Reply Last reply
        0
        • S shiva kore

          Thank you Luc Pattyn. Now I got clear picture . :) ************ S G KORE *******************

          C Offline
          C Offline
          CoderForEver
          wrote on last edited by
          #4

          In addition to the above comment, what I suggest you is that try to draw a picture in a paper while you work on Object types & references so that you will always know what you are doing and have the right picture in your mind. It is very helpful, even in Arrays and Memory Allocations

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups