0

I'm a beginner at programming with little to no experience. I want to learn Java because I feel like it's such a great language to learn from. I've been following the guides on this playlist on youtube here. If you look into it, I'm on the 32nd video and so far Java is very promising

Anyways, I need help understanding this code:

package learningjava;

class apples{
public static void main(String args[]){
    int lucas[]={3,4,5,6,7};
    change(lucas);
    
    for(int y: lucas)
        System.out.println(y);
}

public static void change(int x[]){
    for(int counter=0;counter<x.length;counter++){
        x[counter]+=5;
      }
   }
}

This code is supposed to take the array: int lucas[]={3,4,5,6,7}; and add 5 to each number inside of the array. Then, it's supposed to display the sum of these numbers with 5 added to them; which it does just that, displays 8, 9 ,10,11,12. I just hope that someone could clear up the smoke for me on this one, cause it's hard to follow. I don't understand how array lucas is connected to int x[]. I also don't understand why we need the for loop or what it does.:

for(int counter=0;counter<x.length;counter++){
    x[counter]+=5;
}

This post was poorly made and explained, but I'm desperate at this point to know what exactly is going on in this code. If anyone could give me a thorough explanation, I would REALLY appreciate that!

Community
  • 1
  • 1
  • 2
    The variable names are irrelevant. You are passing a reference to lucas array into the change method, where it is called x. That is still a reference to lucas. – OldProgrammer May 31 '17 at 20:38
  • 2
    Note: use `int[] lucas`, not `int lucas[]`. The `[]` is part of the type, not the variable name. The syntax is supported "as a nod to the tradition of C and C++", but discouraged. – Andy Turner May 31 '17 at 20:47

4 Answers4

1

In Java terms, the int x[] in your code is a method parameter/argument. When you call change(lucas) the x parameter points to the reference of lucas array. Now that its pointing to the lucas array, the for loop below

for(int counter=0;counter<x.length;counter++){
    x[counter]+=5;
} 

iterates through the original lucas array and add the values.

Consider a scenario where the x points to a different reference/memory location inside the method and make the change, none of this would reflect in the actual `lucas' variable.

public static void change(int x[]){
    x = new int[]{3,4,5,6,7};
    for(int counter=0;counter<x.length;counter++){
        x[counter]+=5;
      }
   }
}

That's the key in understanding JAVA's pass-by-value & reference.

Uday
  • 1,165
  • 9
  • 12
1

First, in the function definition

public static void change(int x[]){

x is a formal parameter - you may choose arbitrary name instead of x, as it is just placeholder for the real parameter.

Subsequent commands show what the function will perform with real parameter (so far unknown) when the function will be called by using formal parameter instead of real one.

Now the function is called (used):

change(lucas);

with the real parameter - yes we want change lucas! So the statements in the change() function definition are now performed with lucas instead of x.


Second: Why we need the loop

for(int counter=0;counter<x.length;counter++){
   x[counter]+=5;
}

In your particular code we don't need the loop, nor the function change(). We may use the direct way to add 5 to every element in the array:

lucas[0] += 5      // is it shortened form for lucas[0] = lucas[0] + 5
lucas[1] += 5
lucas[2] += 5
lucas[3] += 5
lucas[4] += 5

But we may meet generally 2 problems:

  1. lucas may have many elements, not only 5.

  2. We want to do the same thing (adding 5 to each element) with other (so far not known) array, e. g. mathew - in future.

So we will solve both of these 2 problems generally - by defining the function change() which doesn´t know in advance the name of an array, nor the number of its items, nor the values of its items. Let x be the placeholder (formal parameter) for that array.

x.length

means "use the attribute (property) length" of that array - every array has this attribute an it is the number of its items.

So, after substituting the real parameter (lucas or mathew) for the formal one (x) we will know the numbers of items of this real array.

We use it in the for loop:

for (int counter = 0; counter < x.length; counter++) {

Here is another placeholder (counter) and again - its name is not important, it may be other one, e. g. i:

for (int i = 0; i < x.length; i++) {

We need this placeholder for indices (one index at a time) of array's element - they are numbered not from 1 but from 0, so the 1st element has index 0, 2nd 1, etc. - so the last one will have index x.length - 1.

How to reach that this placeholder (counter, i, or other arbitrary name) will sequentially have values 0, 1, 2, etc.?

The for statement

for (int counter = 0; counter < x.length; counter++) {

has 3 parts in its header, delimited by ;:

  1. The 1st part defines what to do initially:
    int counter = 0; // set the first value of the placeholder to 0

  2. The 2nd part defines the condition for continuation (of looping):
    counter < x.length; // is the placeholder's value still less than number of items?
    If this condition is not satisfied, the game for for loop is over.

  3. The last part defines what to do (again and again):
    counter++ // increase the counter's (placeholder) value
    after performing commands in the body of the loop (between curly braces {})

Now, in the body of the loop is just 1 statement

     x[counter] += 5;     // Shorter form for x[counter] = x[counter] + 5;

which will be performed again and again by the previous rules (of course, after substituting the real parameter (lucas) for the formal one (x)).

MarianD
  • 13,096
  • 12
  • 42
  • 54
0

Basically what is happening is the array initialization:

int lucas[]={3,4,5,6,7};

You are then calling the function change() and passing an array with the name lucas to it. The function change() is looping through each int in an array with the name x passed to it using a counter for the array index which starts at 0 and increments by 1 until it hits x.length which is 4 in this case. Every iteration it is taking the value of the int in this array and adding 5 to it.

P.S. Java is Pass-By-Value, but it acts similar to Pass-By-Reference when you pass an object - this is because you are passing the reference to the object by value. When you change the value in the array in the change() function, you are also changing the value at all other references to this array object.

Brandon White
  • 997
  • 9
  • 22
  • Technically "everything in Java is Pass-By-Reference" is not true. I understand what you are referring here but I would suggest not to refer this as Pass-By-Reference. – Uday May 31 '17 at 20:54
0

In order to read (print) the elements inside the array, we use loop, your lucas array consists of different numbers so in order to print your numbers inside the array, we use loop.