1

Edit to explain difference between this question and "exact duplicate": The proposed question has many answers, and they are written only to answer a conceptual question. My question is conceptual in nature, but I also asked for specific answer on how to do something functionally. Also, as I do not know what pass-by-reference and pass-by-value mean, how could I understand an answer to a question that I also do not understand? The answers available on the other question are not accessible to a layman who does not know the correct vocabulary. Unfortunately, I know "how" to do a lot of things with Java because I have taught myself, but often do not know the "what" or "why". Answering my question in terms of vocabulary words that I do not understand does not help, and is akin to me reading a textbook (which I have tried), and renders the point of asking the question mute. If you cannot answer it in a manner which the uninitiated can understand, and are unwilling to do so, you are so much hot air and certainly no better than the textbook. Thank you for your consideration.

I was getting a bunch of NaNs in my matrices when doing some operations and decided to run through the NetBeans Debugger. I found the issue, and fixed it but would like to know why it was happening. Here is my original code for a method:

public static double[] row_scalar_mult(double scale, double[] row){
    for(int i=0; i<row.length;i++){
        row[i] = row[i]*scale;
    }             
    return row;
}

What I found was that the row of the 2D array that I passed to this method got acted on directly, which was not the intent. I thought that the method was supposed to just make a copy of it or some such thing and use it without changing what it was given. I fixed it below:

public static double[] row_scalar_mult(double scale, double[] row){
    double[] result = new double[row.length];

    for(int i=0; i<row.length;i++){
        result[i] = row[i]*scale;
    }       

    return result;
}

But I am curious about this happening conceptually, and if there is some simple way to declare a variable or a method that keeps the input of the method intact outside the method. Also, I'm sure there is terminology for all of what I'm saying, but I didn't know what words to use in the search. I hope this isn't a repeat.

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
  • In your first definition of the method `row_scalar_mult`, the parameter `row` (which is a reference to an array of doubles) is copied when your function is invoked. This means that any modification of this **reference** within the method is not passed back to the caller. However, the method code can modify the object that the reference points to (the array of doubles), as you've found out. – David Choweller Nov 07 '16 at 16:15
  • @DavidChoweller Is there a way to keep that from happening by the way I declare my variables or by the way I write my methods? Does it have anything to do with the public, private, static, final keywords? The online articles on these things are very confusing, and I end up just doing what keeps the errors from popping up without actually understanding the why. – rocksNwaves Nov 07 '16 at 16:18
  • 1
    Also, there is a partial answer to your question in that any primitive type (int, short, char, etc.) passed as a parameter of a method cannot be modified by the method. This also applies to the actual reference of a reference type, but not to the object it points to, because the code in the method can use the reference to access the underlying object. – David Choweller Nov 07 '16 at 16:20
  • @DavidChoweller Thank you for being willing to break it down for me. I learned something. – rocksNwaves Nov 07 '16 at 16:29
  • I'm no expert, but I believe that to do what you want, you'd have to put your array in a wrapper class that only allows read access by making the array private and having a getter method that returns the value of the array at a particular index. Unfortunately, then your row_scalar_mult method would then have to know about the class and how to access it. – David Choweller Nov 07 '16 at 16:35
  • @wesleyNeill look for example at this answer of the duplicated question: http://stackoverflow.com/a/12429953/1796579 explaining the concepts in great detail and easily understandable. – Henry Nov 07 '16 at 16:40

0 Answers0