-1

I'm newbie to java. I have a global array defined as private int dataRightTemp[]=new int [4]; and in the middle of the code, this array is being passed to a method as a argument(below I have attached the code). Within this new method the values of the prior global array which is dataRightTemp is being changed from a calculation which uses the passed array in theparameters of the method. When the calculation is finished, and new values are set to the dataRightTemp, how is the values of thearray in parameters of the method be changed, Can anyone help me to figure this out. Thanks in advance.

    public class Demo{    
    private int key[]={1,0,1,0};
    private int dataBlock[]={1,1,1,0,1,0,1,1};
    private int dataLeft[]=new int [4]; // for left part the plain text 
    private int dataRight[]=new int [4];    //for right part of the plain text
    private int dataLeftTemp[];
    private int dataRightTemp[]=new int [4];


     public void roundingStart(){


        for(int i=0;i<4;i++){
            //           AND function
            dataRightTemp[i]=key[i] & dataRight[i];
            //              XOR function
            dataRightTemp[i]=dataRightTemp[i]^dataLeft[i];

        }

        dataLeft=dataRight.clone();
        //      printResults();

        printFirst();
        roundingRest(dataLeft,dataRightTemp);


    }
      public void roundingRest(int leftArray[],int rightArray[]){

        System.out.println("Before");
        for(int x: rightArray){
            System.out.print(x);
        }

        System.out.println();


        for(int i=0;i<4;i++){
            //          AND function
            dataRightTemp[i]=key[i] & rightArray[i];
            //          XOR Function
            dataRightTemp[i]=dataRightTemp[i]^leftArray[i];

        }
        System.out.println("right after");

        for(int x: rightArray){
            System.out.print(x);
        }
        System.out.println();
        System.out.println();
  }
    public void main(String args[]){
     roundingStart();
  }
  }

Following are the received outputs:

Before
0100

After
1011
codsop
  • 9
  • 5
  • 2
    So, are you asking why `dataRightTemp` is changing when it is used in another method? – TheLostMind Jun 06 '16 at 07:11
  • 1
    Hint: try to be consistent: your code should either be working on fields (which you then don't need to pass into methods as parameters); or they are not fields. But mixing that is really confusing. – GhostCat Jun 06 '16 at 07:11
  • @Jägermeister Sorry for aksing, I am a newbie to development. I can't understand what are fields and I don't know how to make the code consistent, Can you provide me a link to learn about them. – codsop Jun 06 '16 at 07:54
  • @codsop Asking is fine. But the thing is: we are taking really basic stuff here. There isn't much sense in "asking" about such things. Instead, you should checkout a good book or online resource, such as https://docs.oracle.com/javase/tutorial/ ... and step by step, walk through that. – GhostCat Jun 06 '16 at 08:07

3 Answers3

2

When the calculation is finished, and new values are set to the dataRightTemp, how is the values of thearray in parameters of the method be changed, Can anyone help me to figure this out.

You are having two references to same array. Those references are 'dataRightTemp' and 'rightArray'

So, any change in array will be reflected in both the references, irrespective of the fact that if it is a global reference in class or local reference in method.

Azodious
  • 13,752
  • 1
  • 36
  • 71
0

When you are calling roundingRest(dataLeft,dataRightTemp); rightArray is dataRightTemp. So, when you change dataRightTemp inside roundingRest function, you change rightArray too. Array parameters come to function as link, not as "copy of source array".

Michael Andreev
  • 362
  • 2
  • 13
0

I agree on the Azodiuos answer. You can use local array to store the values of rightArray Try this code. If I have understood your requirement correctly, I think this will satisfy your need

       public void roundingRest(int leftArray[],int rightArray[]){
            int rightTempTemp[]=new int[4];
    //      for Storing the rightArray[]

    //      for Copying the rightArray to rightTempTemp
            for(int j=0;j<rightArray.length;j++){
                rightTempTemp[j]=rightArray[j];

            }
            System.out.println("Before");
            for(int x: rightTempTemp){
                System.out.print(x);
            }

            System.out.println();


            for(int i=0;i<4;i++){
                //          AND function
                dataRightTemp[i]=key[i] & rightTempTemp[i];
                //          XOR Function
                dataRightTemp[i]=dataRightTemp[i]^leftArray[i];

            }
            System.out.println("right after");

            for(int x: rightTempTemp){
                System.out.print(x);
            }
        System.out.println();
        System.out.println();

}
Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58