0

Okay I have the two java-classes:

public class MaximumNormalize {

    public static void display(double z1, double z2) {
       System.out.println("doubles are "+z1+" and "+z2);
    }

    public static void display(double[] zahlen) {
       System.out.println("double-array with length "+zahlen.length+" :");

       for (int i=0; i<zahlen.length; i++) {
           System.out.println("Element "+i+" is "+zahlen[i]);
       }
    }

    public static double maximum (double z1, double z2) {
       if (z1>=z2) return z1;
       else return z2;
    }

    public static double maximum (double[] zahlen) {
       double ret = 0;
       for (int i = 0; i < zahlen.length; i++) {
           if (ret < zahlen[i]) ret = zahlen[i];
       }
       return ret;
    }

    public static void normalize (double z1, double z2, double diff) {
       z1 /= diff;
       z2 /= diff;
    }

    public static void normalize (double[] zahlen, double diff) {
       for (int i = 0; i < zahlen.length; i++) {
           zahlen[i] /= diff;
       }
    }
}


public class TestMaximumNormalize {
    public static void main (String[] args) {
       double[] meineZahlen = {6.5, 7.0, 6., 5.5, 6.5, 7.5};
       double x1 = 10.5;
       double x2 = 9.5;

       System.out.println("before:");
       MaximumNormalize.display(x1, x2);
       MaximumNormalize.display(meineZahlen);

       double maximumEinzeln;
       maximumEinzeln = MaximumNormalize.maximum(x1, x2);
       MaximumNormalize.normalize(x1, x2, maximumEinzeln);

       double maximumVomArray;
       maximumVomArray = MaximumNormalize.maximum(meineZahlen);
       MaximumNormalize.normalize(meineZahlen, maximumVomArray);

       System.out.println();
       System.out.println("after:");
       MaximumNormalize.display(x1, x2);
       MaximumNormalize.display(meineZahlen);
      }
}

Why does the double-array store the normalized values? I would have expected that nothings changes since the normalize-method is a void. However, the array receives the new values, the doubles don't.

Dan
  • 2,701
  • 1
  • 29
  • 34
Lotzki
  • 489
  • 1
  • 4
  • 18
  • I recommend reading http://stackoverflow.com/questions/40480/is-java-pass-by-reference which explains in detail why Java is **not quite** pass by reference, but something close to it. – Dawood ibn Kareem Nov 26 '13 at 02:42

2 Answers2

5

In the normalize (double[] zahlen, double diff) method, you are passing an array of doubles, that in Java are passed by reference. So if you modify it inside the method, it will modify the original array.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • Ah okay, I see. So whenever you pass an array you give a reference to the stored values. If you pass a regular double, it will create a copy of it in the ram? So changing the double will only change the copy. The reference points to the original point where the values are stored and therefore change them? – Lotzki Nov 26 '13 at 01:36
  • Of course, it's not quite correct to say that _anything_ in Java is passed by reference. Java is always passed by value - it's just that the values that are passed are often references. So this answer is "mostly right", but not perfect. See the link in my comment under the question for more detail. – Dawood ibn Kareem Nov 26 '13 at 02:45
2

In Java, all array variables are references. Those references are passed by value, in your code above. If you want to operate on a copy of the array, you have to make that copy explicitly.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110