0

I'm very new to Java and I don't understand why the value of the variable "A" changes when passed as argument to a function. I do change it's value in the function but I think this shouldn't be a problem since the function creates its own "copy" of the argument. My project consists of a main function and a class in which my functions are declared and implemented as shown below.

    package NewtonRapson;

    public class Main {

        public static void main(String[] args) {


            division2 d2 = new division2();


            double A[] = {1, -2, 0, 7};
            double B[] = {0,1,1};
            double D[];
            double res = 0;

            d2.imprimir(A);
            res = d2.p_eval (A, 0.707);
            System.out.println();
            System.out.println( res);


            d2.p_div(A,B);

            d2.imprimir(A);
            res = d2.p_eval (A, 0.707);
            System.out.println();
            System.out.println( res);


        }

    }
`

    package NewtonRapson;
    public class division2 {
        int grado(double A[]){
        int grado = A.length -1;
        int cont = 0;

        for (int i = (A.length - 1); i>= 0; i--){
            if (A[i] != 0){break;}
            if (A[i] == 0){ cont++; }



        }
        return grado - cont;
        }

        double [] incrementar_grado(double A[], int k){
            double r[] = new double[A.length];
            for (int i = 0; i < A.length; i++){
                r[i] = 0;       
             }

            for (int i = 0; i < A.length; i++){
                if (i+k < A.length){
                    r[i+k] = A[i];
                }       
             }
        return r;
    }

        double [] p_div(double A[], double B[]){
            double cte;
            double C[] = new double[A.length];

            while (grado(A) >= grado(B)){

                for(int i = 0; i < C.length; i++){
                    C[i] = 0;

                }
                cte = A[grado(A)] / B[grado(B)];

                    for(int i = 0; i < C.length; i++){
                        if(i >= B.length) break;
                        C[i] = -cte*B[i];


                    }
                    for(int i = 0; i < C.length; i++){
                            if (grado (A) > grado (C)){
                            C = incrementar_grado( C, grado(A)-grado(C) );
                            }


                        A[i] = A[i]+C[i];
                    }
            }
            double res[] = new double[grado(A) + 1];
            for (int i =0; i< res.length; i++){
                res [i] = A[i];

            }
            return res;

        }

        double p_eval(double A[], double x) {
            double res = 0;
            for (int i = 0; i < A.length; i++) {
                res = res + A[i] * Math.pow(x, i);

            }

            return res;
        }

        void imprimir(double A[]){
            System.out.println();

            for(int i=0;i<A.length;i++){
                //System.out.println( dividir(polinomio,d)[i] );
                System.out.print(  A[i] );
                System.out.print(" | ");

            }
        }

        double [] p_derivar(double A[] ) {

            double d[] = new double [grado(A)];
            for(int i=0;i< d.length; i++){
                d[i]= A[i+1] * (i+1);
            }

        return d; 


        }

    }

The reason as to why A changes its value still remains unknown to me.

Vega
  • 27,856
  • 27
  • 95
  • 103
avm
  • 101
  • 2
  • 1
    When you say that `A` changes value, I am assuming you mean the contents of `A`, is that correct? See the first answer to this question - https://stackoverflow.com/questions/12757841/are-arrays-passed-by-value-or-passed-by-reference-in-java as well as the question I suggested as being a duplicate. – pstrjds Oct 21 '17 at 20:36
  • @pstrjds the second answer was helpful. – avm Oct 21 '17 at 20:57

1 Answers1

1

double A[] is a table (object), not a simple type variable. To the function p_div(double A[], double B[]) you are passing only a reference to tables A and B from the main function. When you are changing A (A[i] = A[i]+C[i];) in p_div you are changing the A in main because they are (point to) the same thing.

Em.
  • 71
  • 4