-2

I'm noob in programming, now learning Java. I have read that Strings are immutable in Java. But, I have a question regarding it.

public class Immutable {

    public static void main (String[ ] args) 
    {
        new Immutable().run(); 
    } // method main

    public void run() 
    {
        String s = "yEs";
        String p = "Do";

        p=p.toUpperCase();
        s=s.toLowerCase();
        //Here above I'm able to change the content of the string object. 
        //Why is it said Immutable??

        p = new String("DoPe");

        System.out.println(p);
        // Here I'm taking it as I'm creating new object and I'm assigning it to 'p'
        //rather than changing the previously assigned object. so it's immutable

        //Is my understanding correct??


        flip (s); 
        System.out.println(s);
        //Why does assigning a new object using a method doesn't work
        //while if I do it explicitly, it works(as I've done for 'p', see above)

    } // method run

    public void flip (String t) 
    {
        System.out.println(t);
        t = new String ("no");
        System.out.println(t);

        t = "nope";
        System.out.println(t);
    } // method flip

} // class Immutable

Please see questions in my comments.

Revision:

public class Immutable {

    public static void main (String[] args) 
    {
        new Immutable().run(); 
    } // method main

    public void run() 
    {
        String s = "yEs";
        String p = "Do";

        int [] arr = new int[5];

        for (int i = 0; i < 5; i++)
            arr[i] = i;

        System.out.println("Value in the calling function before the altering is done: "+Arrays.toString(arr));

        alter(arr);
        System.out.println("Value in the calling function after the altering is done: "+Arrays.toString(arr));

        p = p.toUpperCase();
        s = s.toLowerCase();
        //Here above I'm able to change the content of the string object. 
        //Why is it said Immutable??

        p = new String("DoPe");

        System.out.println(p);
        // Here I'm taking it as I'm creating new object and I'm assigning it to 'p'
        //rather than changing the previously assigned object. so it's immutable

        //Is my understanding correct??

        System.out.println("Value of string in the calling function before the altering is done: "+s);
        flip (s); 
        System.out.println("Value of string in the calling function after the altering is done: "+s);
        //Why does assigning a new object using a method doesn't work
        //while if I do it explicitly work(as I've done for 'p', see above)

    } // method run

    public void flip (String t) 
    {
        System.out.println("Value of string in the called function, before altering: "+t);
        t = new String ("no");
        System.out.println("Value of string in the called function, after Altering: "+t);

        t = "nope";
        System.out.println(t);
    } // method flip

    public void alter(int[] a)
    {
        System.out.println("Value in the called function, before altering: "+Arrays.toString(a));
        a[3] = 50;
        System.out.println("Value in the called function, after Altering: "+Arrays.toString(a));
    }

} // class Immutable

Modification works for arrays, but not for strings. Is this the reason strings are called immutable??

Am I missing something?

msrd0
  • 7,816
  • 9
  • 47
  • 82
  • 1
    `p=p.toUpperCase();` there is a difference between changing state of object, and assigning new object. – Pshemo Oct 05 '14 at 17:32
  • 1
    possible duplicate of [Immutability of Strings in Java](http://stackoverflow.com/questions/1552301/immutability-of-strings-in-java) – Svetlin Zarev Oct 05 '14 at 17:39

5 Answers5

2

Here above I'm able to change the content of the string object. Why is it said Immutable??

You don't change the contents of the String, you create a new String object and link it with the variable, the old object will be removed.

Here I'm taking it as I'm creating new object and I'm assigning it to 'p' rather than changing the previously assigned object. so it's immutable. Is my understanding correct??

Yes, and this is actually the same you did one question above, but the String object was created by one of its instance methods.

Why does assigning a new object using a method doesn't work while if I do it explicitly work(as I've done for 'p', see above)

This is because the immutability of String. Java is pass-by-value, but your method needs to be pass-by-pointer. Since you can't change the contents of the String (you only can create a new String object), you'll need to return the String.

msrd0
  • 7,816
  • 9
  • 47
  • 82
1

You're not changing the content of the string object, you're changing which variable point to which object in memory.

Variables are just pointers to the objects in memory, not the actual objects.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
1

You're not changing the content of the string objects. You're creating new objects and making s and p point to the new objects.

user253751
  • 57,427
  • 7
  • 48
  • 90
1
//Here above I'm able to change the content of the string object. 
//Why is it said Immutable??

You did not change the content of the string, you simply created a new String and assigned its reference to your String variable. That also answers to your second comment.

//Why does assigning a new object using a method doesn't work
//while if I do it explicitly work(as I've done for 'p', see above)

Because Java methods are not pass-by-reference. See this question for further information.

Community
  • 1
  • 1
Dici
  • 25,226
  • 7
  • 41
  • 82
  • Java Methods aren't pass-by-reference, but a reference won't help, you would need a pointer – msrd0 Oct 05 '14 at 17:36
0

I believe you are making confusion about things. Overall, I do understand what you mean. You believe that you "changed" p by p = new String("DoPe"); which is let's say correct but the same thing doesn't happen with s when you call flip (s); Well, calling it won't "change" s because your function flip only takes a string and modifies it internally and that's it. In order to "change" s using a function you have to do this modifications:

1 s= flip (s); (s receives the returned value of the function) 2. Function flip should return a String and not a void

public String flip (String t) // String instead of void 
    {
        System.out.println(t);
        t = new String ("no");
        System.out.println(t);

        t = "nope";
        System.out.println(t);
    return t; //returns the String t
    }
Stefanel S
  • 45
  • 2