Why change in one StringBuffer object affects the other? does these have shared memory

328 Views Asked by At

I am new to java. I am trying to do my homework. In this I first made the StringBuffer objects strB1 and strB2. After taking inputs from the user. I made a new StringBuffer object and copied the content of the strB1 to that object. I am making all the modification to new stringbuffer object strobj that contains the content of strB1. But changes in the new object is reflecting in the orginal. Please help. I am not able to understand why the orginal object is being changed.

You will see at the end of the code printing both the objects is yielding same result whereas i am making changes in the one object only.

import java.util.Scanner;

public class Homwork1_Q2 {

    StringBuffer strobj2;

    Homwork1_Q2()
    {

    }
    // Copy constructor used in part 2 and 3 or hw
    Homwork1_Q2(StringBuffer strobj_2)
    {
        this.strobj2 = strobj_2;
    }

    public static void main(String args[])
    {
        // create two StringBuffer objects
        StringBuffer strB1 = new StringBuffer();
        StringBuffer strB2 = new StringBuffer();

        //1. create a obj of Scanner and take input in STRB1
        Scanner scan = new Scanner(System.in);
        System.out.println("Input the  Long String");
        strB1.append(scan.nextLine());

        //Input a shorter String
        System.out.println("Input the  Short String");
        strB2.append(scan.nextLine());

        //If 2nd stringBuffer is longer the first through an
        //exception and again take the input
        try
        {
        if(strB1.length() < strB2.length())
        {
            throw new Exception();

        }
        }catch(Exception e)
        {
            System.out.println("2nd String should be shorter.. Input again");
            strB2.append(scan.nextLine());
        }


        // 2. Create a StringBuffer object from the long String
        StringBuffer strobj = new StringBuffer();
        strobj = strobj.append(strB1.toString());

        //3. Using the StringBuffer with the appropriate specific constructor.
        Homwork1_Q2 object = new Homwork1_Q2(strB1);

         //4. Position of the small string in the long string
        //If more then one position is present then it will calculate that too 
        int position;
        int check = 0;

        while((strobj.indexOf(strB2.toString()))!=(strobj.lastIndexOf(strB2.toString())))
        {
            position = strobj.indexOf(strB2.toString());
            System.out.println("Small String is present at position "+ (position+check));
            strobj.delete(position, position+strB2.length());
            check = check+strB2.length();
        }

        position = strobj.indexOf(strB2.toString());
        System.out.println("Small String is present at position "+(position+check));
        strobj = strB1;

        //5. Delete the small string
        //If more then one time small string is present in large string then it will delete them too 
        while((strobj.indexOf(strB2.toString()))!=(strobj.lastIndexOf(strB2.toString())))
        {
            position = strobj.indexOf(strB2.toString());
            strobj.delete(position, position+strB2.length());
            check = check+strB2.length();
        }

        position = strobj.indexOf(strB2.toString());
        strobj.delete(position, position+strB2.length());
        check = check+strB2.length();
        System.out.println(strobj.toString());

        System.out.println(strB1.toString());


    }
}

enter image description here

2

There are 2 best solutions below

1
On

To copy you don't want to use the assignment operator. Instead you want to use the copy constructor like this:

strobj = new StringBuffer(strB1);

You also use the assignment operator in the copy constructor for Homwrk1_Q2

Homwork1_Q2(StringBuffer strobj_2)
0
On

You are not only copying the content of the StringBuffer but you are also copying the reference of the StringBuffer object to some other StringBuffer. What this means according to your code is that both the variables strobj,strB1 are pointing to the same memory location. Changing one of them will change the other one too. I hope that helps. Do tell me about your thought process!