0

Is the order in which arguments are passed into a method (in Java) defined? For example, in the code segment

Arrays.equals(ascendingSegment, Arrays.sort(ascendingSegment))

Which is used to check if an array is in fact ordered ascending, am I guaranteed that the original array is passed in before the sort method is called on it?

EDIT: Seems like there's a bit of a misunderstanding. Does the equals method get a copy of the ascendingSegment before it is sorted, or is the sort called first, before the first argument is passed? Given an array such as {1, 5, 2, 4}, will the equals method receive {1, 5, 2, 4} and {1, 2, 4, 5} as arguments or two copies of {1, 2, 4, 5}?

FeifanZ
  • 16,250
  • 7
  • 45
  • 84

3 Answers3

6

If you step through it with a debugger and step through it, you will see that all the arguments are evaluated, then the function is called.

I would also say that trying to do what you posted is a bad practice. When testing an object, you should not be performing any operation on that object that would mutate it. After that method call, you would not be able to show the order of the list before the test.

Edit: Arrays.sort() does not return a value, it sorts in place. Therefor you can't use it like this. You should use Arrays.copyOf() to make a copy of the array, sort the copy, then compare the two arrays.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
  • I'm a novice at Java...no idea where to find the debugger or how to use it. :( – FeifanZ Apr 22 '11 at 15:14
  • 1
    @Inspire48: Are you using an IDE like Eclipse? It has one built in. – unholysampler Apr 22 '11 at 15:17
  • I've played around in Eclipse, but I'm not doing any serious Java programming, so I'm mostly using NotePad++ with a macro to invoke `javac` and `java`. – FeifanZ Apr 22 '11 at 15:21
  • @Inspire48: You are putting yourself at a serious disadvantage. Just because you are not writing a program you plan on giving to other people doesn't mean you should not be using the best tool for the job. – unholysampler Apr 22 '11 at 15:25
  • 1
    You might be right, but it's easier to learn Java when I don't have an IDE to distract me. Let's not turn this into an IDE war, shall we? – FeifanZ Apr 22 '11 at 15:28
  • @Inspire48 - JDK installations include a Java debugger that you can run from the command line: http://download.oracle.com/javase/6/docs/technotes/tools/solaris/jdb.html – Stephen C Apr 22 '11 at 15:37
  • Coming from an Objective-C background, NSArray's sort methods don't sort in place. That seems more reasonable to me, rather than sorting in place. I realize that Java sorts in place, but conceptually it didn't quite click. – FeifanZ Apr 22 '11 at 15:48
  • @Inspire48 - given that most general purpose sort algorithms are sort in place, the Java API is more efficient, as it doesn't involve making a copy. – Stephen C Apr 22 '11 at 15:57
1

Java follows Pass-by-value.

Pass-by-value means that when you call a method, a copy of the value of each actual parameter is passed to the method. You can change that copy inside the method, but this will have no effect on the actual parameter.

So in your example, your Array will first be sorted and then passed through to the Array.equals method.

Your confusion may be because you are thinking in terms of Pass-by-reference. Here's a question explaining why Java is Pass-by-value: Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
KushalP
  • 10,976
  • 6
  • 34
  • 27
  • Yes, but in these cases the array is actually a pointer, not an actual value. Therefore, the value of the pointer will be copied, but it is still pointing to the same array – FeifanZ Apr 22 '11 at 15:13
  • Indeed, but Java expects a value to appear for every argument. For this reason, all arguments in your example are evaluated before they are passed. – KushalP Apr 22 '11 at 15:17
  • Thanks. That's going somewhere. But is the first argument copied before sorting? In other words, is the copying of the argument's value a separate step from the actual passing? – FeifanZ Apr 22 '11 at 15:22
  • Ah, Array.sort() does the sort in-place. In this case, you should probably make a copy of the array and pass it into your method. – KushalP Apr 22 '11 at 15:28
1

Firstly, your example won't compile because Arrays.sort(...) doesn't return anything.

Secondly, if (hypothetically) Arrays.sort(...) did return something, then it would depend on what it returned.

  • If the (hypothetical) Arrays.sort(...) returned a sorted copy of the original array, then

        Arrays.equals(ascendingSegment, Arrays.sort(ascendingSegment));
    

    would be called with two different array arguments; i.e. the original array and the sorted copy.

  • If the (hypothetical) Arrays.sort(...) returned the original array after having done an in-place sort, then

        Arrays.equals(ascendingSegment, Arrays.sort(ascendingSegment));
    

    would be equivalent to this:

        Arrays.sort(ascendingSegment);
        Arrays.equals(ascendingSegment, ascendingSegment);
    

Note:

  1. Java call parameters are passed by value, though the value that is passed for a reference type (i.e. an object or an array) is a reference. This means that when an array argument is passed, you are actually passing the array reference, and the called method gets the original array, not a copy.
  2. The argument expressions for a procedure call are evaluated from left to right.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for that—didn't catch the compile issue there, this being a hypothetical example. And that's a very well-written explanation. – FeifanZ Apr 22 '11 at 15:59