Introduction =====
Ask HN: Who has
Q: Creating an An
The invention is c
Phoenix (Ariz.) Un
// Copyright 2013
Introduction {#s1}
Q: Python convert
As the number of c
Category Archives:

Q: When is the nu
You are here Jazz
Therapeutic effect
Woolly monkeys, in
An analysis of the
YouTube user Dr. D
Introduction =====
Introduction {#Sec
Q: Proving for $E
Q: How to prevent
Q: Does the order matter for variables in java? I was wondering if this would produce different results: int[] array= {4,8,6,3}; System.out.println(array[0] * (array[1] - array[0])); VS System.out.println(array[0] * (array[1] - array[0])) When I test this I get 9 as the output A: In java it doesn't have to care about order. This is one of the reason we have functions like min, max and sort in array because we know that java arrays are zero-indexed. A: No, array indices are not compared alphabetically. They can be compared, but they are only equal to each other if and only if their elements are equal to each other. A: No, it does not. The two examples have the same output. That is because Java is an Object-Oriented Language, so arrays in Java are objects like all other objects. A: In java, [0] is the same as saying 0 because the array index is zero-based. I suppose you're asking if the order of operations matters in this case. Yes, the order does matter. The order of the multiplication is the same for each line, but the order of the subtraction is not. The order of operation matters in java because: the expression is a variable and the variable is assigned using a value of another expression which might be evaluated in different order. For example in these two lines of code: int a = f(b); int b = f(a); the order of evaluation of a and b can be either a to b or b to a. Because the variable names have no syntactic meaning on their own, this is ambiguous. A: No. They are syntactically equivalent and will produce the same result. The compiler is allowed to evaluate the expression in any order and use that result to perform the assignment. Does the order matter? No. It does not matter because arrays are objects in Java. They do not have a natural order like that of numerical arrays and it is allowed for expressions and assignment to have any order. What happens if I change the order of operations to System.out.println((array[0] - array[1]) * array[0]); Is it guaranteed that it will still produce the same result? No. Different order of operations will produce a different result. It's allowed for the compiler to produce different results, though. Is it less-efficient than the other? Yes, if you are thinking about efficiency. Is it a good programming style? No, because it leads to brittle code with unintended consequences. It may be appropriate when used as a temporary hack to get something working, but usually the best solution is to not even write such code in the first place.