0

I have a doubt here. I am learning Java and I see a behavior that I don't understand. I read java in pass by value and the original variable shouldn't change its values when you send it to a method (or that is what I understood so far).

So, I will add here two cases that are very alike in my point of view. First case the variable doesn't change its value. Second case, the variable changes it's value.

First Case (Variable stays the same way):

package Chapter10.Part6;

import java.util.Scanner;


// Important: Pass by Value!

public class Chap10Part6 {

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        int number;

        System.out.print("Enter a Interger: ");
        number = input.nextInt();

        System.out.println("Number squared: " + square(number));
        System.out.println("Original Number: " + number);


        input.close();

    }


    static int square(int num){
        num = num * num;
        System.out.println("Number in square method: " + num);
        return num;


    }
}

Second Case (Variable changes):

package Chapter15.Part4;

import Chapter15.FunctionsPart1;

public class Chap15Part4 {

    public static void main(String[] args) {

        final int rows = 10;
        final int columns = 5;
        int [][] table = new int[rows][columns];

        FunctionsPart1.build2DArray(table, rows, columns);
        FunctionsPart1.display2DArray(table, rows, columns);


        System.out.println("");
        FunctionsPart1.organize2DArray(table, rows, columns);
        FunctionsPart1.display2DArray(table, rows, columns);
   }
}



package Chapter15;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class FunctionsPart1 {


    public static void build2DArray(int[][] arr, int rows, int columns){
        Random rand = new Random(System.currentTimeMillis());
        for (int i = 0; i < rows; i++){
            for (int j = 0; j < columns; j++){
                arr[i][j] = rand.nextInt(101);
            }
        }
    }


    public static void display2DArray(int[][] arr, int rows, int columns){
        for (int i = 0; i < rows; i++){
            for (int j = 0; j < columns; j++){
                System.out.print(arr[i][j] + " ");
                if((j+1)%columns == 0)
                    System.out.println("");
            }
        }
    }


    public static void organize2DArray(int[][] table, int rows, int columns){
        int t = 0;
        for(int x = 0; x < rows; x++)
        {
            for(int y = 0; y < columns; y++)
            {
                for(int i = 0; i < rows; i++)
                {
                    for(int j = 0; j < columns; j++)
                    {
                        if(table[i][j] > table[x][y])
                        {
                            t = table[x][y];
                            table[x][y] = table[i][j];
                            table[i][j] = t;
                        }
                    }
                }
            }
        }
    }


}

Could anyone explain me why?

Thanks for the time.

  • 3
    The variable is passed by value. The content of the variable is a reference to an array. The array itself is not copied. So it is possible it to find that it is modified by a receiving function. – khelwood Apr 04 '16 at 14:36
  • In short, arrays are treated as objects when they are passed to a method. Objects are passing the value of the reference. Though invoking or working on this reference will reflect in changes to the original object. – SomeJavaGuy Apr 04 '16 at 14:39
  • The `table` variable doesn't change. In neither example does the variable change. You might be changing the data the `table` points to, but this is not the same as changing the variable. – Peter Lawrey Apr 04 '16 at 14:45
  • Thanks everyone who answered me. – Luiz Conrado Apr 04 '16 at 14:51

0 Answers0