0

I would appreciate some help with the following, asked previously, not addressed in older questions related to Java and references.

Java is pass by object reference, years ago we called references, pointers. I am assuming this in predicting the output, however, even when I make tortVals and vraiVals aliases of each other, and expect for v1[1] and v2[1] to be the same, they are not. v2[1] still has a value of 6. I have not seen this addressed in existing issues, and still am asking for help. Same thing happens with the array of strings - the array arguments are pointers, yet the changes expected do not follow. Specifically, v2[1] remains 6, v3[1] is set to 'huit', instead of what I would expect, 'trois'. Thanks, Jacques

Here is my prior question: I have a small sample piece of code below - I predicted the output (print) would be 5 5 trois, because of the references. I got it wrong, it's 5 6 huit. Given that references allow for the area 'pointed to' to be changed, what am I missing? Help much appreciated, obviously this is a test case for a bigger scenario.

 public static void main(String[] args) {
        essayer();
    }

    public static void resetVals(int[] vraiVals, int[] tortVals, String[] noms) {
      vraiVals[1] = 5;
      tortVals = vraiVals;
      noms[1] = new String("huit");
      noms = new String[3];
      noms[1] = new String("trois");
    }

    public static void essayer() {
        int[] v1 = {1,2,3};
        int[] v2 = {5,6,7};
        String[] v3 = {"un", "deux", "trois"};
        resetVals(v1, v2, v3);
        System.out.println(v1[1] + " " + v2[1] + " " + v3[1]);
    }
Jake
  • 9
  • 1
  • 1
    "Java is pass by object reference" -- the exact opposite. Java is pass by value – Hovercraft Full Of Eels Jan 14 '20 at 01:21
  • See: [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Hovercraft Full Of Eels Jan 14 '20 at 01:22
  • `vraiVals[1] = 5;` updates `v1[1]` --- `tortVals = vraiVals` makes parameter variable refer to `v1`, but you never use `tortVals` again, so statement has no effect --- `noms[1] = new String("huit")` updates `v3[1]` --- `noms = new String[3]` makes parameter variable refer to new array, so next statement doesn't affect `v3` – Andreas Jan 14 '20 at 01:25
  • Don't use `new String("some literal")`, just use the literal directly. There is really never a good reason for calling `new String(String s)`. – Andreas Jan 14 '20 at 01:27
  • True, Java is pass by value, but when you pass a reference, it is termed pass by object reference - meaning that changes can be made to the object. Thanks Andreas, I understand the new array being distinctly different for the strings, appreciate you pointing this out. However, I do not understand why v2[1] is not set to 5, as I have made tortVals and vraiVals aliases of each other, which (I think) means that v1 and v2 should be pointing to the exact same area of memory. Yet, v2[1] is still set to 6 when I do the print? Thanks for any further insight into v2 hanging onto old values. – Jake Jan 14 '20 at 15:06

0 Answers0