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.