-3

So I have this method that takes an (ArrayList, int n, int r), let's just say that the method is supposed to print something if given those args.

but for some reasons, if my main is like this:

public static void main(String[] args) {
    
    ArrayList<Double> array = new ArrayList<Double>();
    int r = 2;
    int n = array.size();
    
    array.add(4.0);
    array.add(5.0);
    array.add(7.0);
    array.add(8.0);
    array.add(10.0);
    
    print(array, n, r);
}

it doesn't work, or at least it doesn't do what it's supposed to do.

BUT if my main method is like this:

public static void main(String[] args) {
    
    ArrayList<Double> array = new ArrayList<Double>();
    array.add(4.0);
    array.add(5.0);
    array.add(7.0);
    array.add(8.0);
    array.add(10.0);
    
    int r = 2;
    int n = array.size();
    
    print(array, n, r);
}

it works perfectly fine.

Does the location of where I created those integers have any effect on my code? Any explanation on why this happens?

ps: I'm fairly new on programming, so if this is actually really simple... I apologize for not knowing.

dullus
  • 1
  • 3

1 Answers1

0

Where you put the declarations?

Those don't matter.

But where you invoke the calls, that matters: Java is, like most programming languages, imperative: Code is executed at line 1, and then line 2, sequentially (or at least, the system acts as if that's how it works, even if internally it optimizes a bit; it may reorder but only if that doesn't have any effect on what you can observe).

So, in your first snippet, the line:

int n = array.size();

means n is now 0.

In the second snippet, you run it after your add statements, so n is 5.

Imagine this code:

ArrayList<Double> array = new ArrayList<Double>();
System.out.println(array.size());
array.add(4.0);
System.out.println(array.size());
array.add(5.0);
System.out.println(array.size());
array.add(7.0);
System.out.println(array.size());
array.add(8.0);
System.out.println(array.size());
array.add(10.0);
System.out.println(array.size());

This would produce:

0
1
2
3
4
5

and I bet it's fairly obvious why it does that.

When you write int n = array.size(); at the top of the method, now you know: That means n is 0. It doesn't live-update. Let's pull on this thread some more:

int n = foo();

is syntax sugar. It's a convenient, shortened way to write something longer, but it means the same thing as the longer form. The longer form being:

int n;
n = foo();

where int n; is declarative and doesn't associate with any actual runtime behaviour; that is merely decreeing: "I decree that this method has a variable; it is named 'n', and it holds some integer value." - the n as a name now exists in the entire method.

n = foo(); on the other hand is associated with code: This is: Invoke the foo() method, and make 'n' be whatever foo() returned. It is not a live view; it is not saying: "n is an alias for invoking foo()". In that sense, the = operator in java is not like the = concept in basic arithmetic. If you want, it makes more sense to read = as 'is now assigned the value of', and not to read it as 'is equal to'. n = foo(); means: n is now assigned the value of foo(), not n equals foo().

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37