TheWiz
TheWiz

Reputation: 115

Need for a temporary variable

I'm new to Java, just started a couple of days ago, from scratch. I tried a Matrix program to understand the functioning of the "for" loop,using Eclipse IDE.

package day1.examples;

public class MatrixClass {

  public static void main(String[] args) {
      // TODO Auto-generated method stub

      int[][] a = new int[4][3];

      int temp=15;
      int i = 0;
      int j = 0;
      a[i][j]=0;
      for (i = 0; i < 4; i++) {
          System.out.println();
          for (j = 0; j < 3; j++) {
              a[i][j]=temp;
              temp = temp + 15;
              System.out.print(a[i][j] + " ");

          }

      }

  }

}

Output:

15 30 45 
60 75 90 
105 120 135 
150 165 180

The above program works well and I get the desired output. What I do not understand is the need for a temporary variable. Why isn't it possible to use the existing variable itself,for increment? When I try increasing the existing variable (a[i][j]),why is there no increment in the value in the output,even though the code gets executed?

package day1.examples;

public class MatrixClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int[][] a = new int[4][3];

        //int temp=15;
        int i = 0;
        int j = 0;
        a[i][j]=0;
        for (i = 0; i < 4; i++) {
            System.out.println();
            for (j = 0; j < 3; j++) {
                //a[i][j]=temp;
                a[i][j] = a[i][j] + 15;
                System.out.print(a[i][j] + " ");

            }

        }

    }

}

Output:

15 15 15 
15 15 15 
15 15 15 
15 15 15

Upvotes: 2

Views: 1654

Answers (4)

Your a[i][j] = a[i][j] + 15; line inside the last of your examples takes the a[i][j] value, and increases it by 15. You have nowhere in your code initialized values of your matrix, so all values are 0.

When you understand that a[i][j] points to a new variable every time i or j changes, you will understand everything.

This means all values in the matrix are 15.

I'd make the loop like this:

for (int i = 0; i < 4; i++) {
    System.out.println();
    for (int j = 1; j <= 3; j++) {
        a[i][j-1] = 15 * (j + (i * 3))
        System.out.println(a[i][j-1] + " ");
    }
}

Upvotes: 2

durron597
durron597

Reputation: 32323

Arrays are initialized by default to their default values. Read this:

Therefore, when you add 15 to each cell, you have a[i][j] = 0 + 15;


What you've called a temporary variable has the language buzzword accumulator (definition #7).

A register in a calculator or computer used for holding the intermediate results of a computation or data transfer.

This value changes each time you go through the loop, which is why the values come out different. If you don't use a temporary variable, you would have to compute the results from the values of i and j every time (in this case, using a formula like (i * 3 + j) * 15)

Upvotes: 3

Mustafa sabir
Mustafa sabir

Reputation: 4360

In the first case you declare a temp variable as 15 and increase its value by 15 each time you store it in array, Whereas in the second case you are taking value of a[i][j] and since a[i][j] is only initialized for a[0][0] and not for others , so according to the language standard it automatically gets initialized to the arrays primitve type , which in this case is int and for int the default is zero. Hence each time only 0+15 i.e 15 is displayed.

Upvotes: 1

BlueMonkMN
BlueMonkMN

Reputation: 25601

The key is that temp is not cleared when moving to the next element, which allows it to accumulate the value from all the cells. If you wanted to do this without a temporary variable, you would need to somehow copy the value from one element to the next before adding 15.

Upvotes: 0

Related Questions