I'm making my own Java matrix program for a linear algebra class and can't seem to stop passing my arrays by reference. I want each method to return a matrix (thus performing calculations based on the original) without altering the original matrix values. Here are some bits from my program below:
public class Matrix{
    private Double[][] data;
    private String name;
    public Matrix(Double[][] newdata){
        this.data = newdata.clone();
        this.name = "default";
    }
    public Matrix scalarMultiply(Double scale){
        Matrix output = new Matrix(this.data.clone());
        output.name = scale + "(" + output.name + ")";
        for(int i = 0; i < output.data.length; i++){
            for(int j = 0; j < output.data[i].length; j++){
                output.data[i][j] *= scale;
            }
        }
        return output;
    }
}
public class Main {
    public static void main(String[] args) throws Exception{
        new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
        Matrix testMatrix = new Matrix(new Double[][]{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}, {100.0, 101.0, 102.0}});
        testMatrix.printSpacedAuto();
        breakLine();
        Matrix testMatrixRestult = testMatrix.scalarMultiply(2.0);
        testMatrixRestult.printSpacedAuto();
        breakLine();
        testMatrix.printSpacedAuto();
        testMatrixRestult.printSpacedAuto();
        breakLine();
    }
    public static void breakLine(){
        System.out.println();
    }
}
I tried using a.clone() in a few different spots but my results seem the same; can anyone see what I am misunderstanding?
