1

I have a class in a Java project which contains a 2D array of values and provides useful methods for interacting with it. It uses Location objects (which just store two values) to access points in the mapping and return the value. It has a method isValid(), which checks whether the Location object is within the mapping of the grid.

public boolean isValid(Location loc) {
    try {
        // will throw an ArrayIndexOutOfBoundsException if not valid
        // occs is the 2D array within the class
        occs[loc.getX()][loc.getY()];
        return true;
    }
    catch (ArrayIndexOutOfBoundsException e) {
        return false;
    }
}

Now, I know I could just use comparisons to check values and see if that loc fits within the grid, but this is faster as it does not need to run comparisons and the cases of non valid Locations being checked are far in the minority. However, here is my problem: since the value returned by the line accessing the array is not being assigned or sent into a method, I get an error about AssignmentOperators. I know it would be an easy fix to either assign it to a temp value or run it through some useless method, but both of those seem like derpy bandaid solutions to me, and I was wondering if there was a cleaner way to do that. (Besides, the former elicits a warning from Eclipse.)

Anyway, this is low priority, but it's bugging me that something so simple is not possible in such an easy implementation to be perfectly honest. Thanks!

assylias
  • 321,522
  • 82
  • 660
  • 783
Corey Noel
  • 403
  • 4
  • 7
  • A question regarding your way of checking location: http://stackoverflow.com/questions/18872733/is-it-ok-to-use-exceptions-to-check-for-array-boundaries/18872734 – Dariusz Sep 18 '13 at 12:54

2 Answers2

2

this is faster as it does not need to run comparisons.

Of course it needs to 'run comparisons'. How else could it possibly work? Your reasoning upon this point is fallacious. Your question is therefore null and void.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • In addition, even if it didn't need the comparison (e.g. somehow relies on hardware traps), throwing an exception would still be far slower than four comparisons. – Sebastian Redl Sep 18 '13 at 11:57
1

Unfortunately, this is just the way Java is. From JLS section 14.8, Expression Statements:

Unlike C and C++, the Java programming language allows only certain forms of expressions to be used as expression statements. Note that the Java programming language does not allow a "cast to void" - void is not a type - so the traditional C trick of writing an expression statement such as:

(void)... ; // incorrect! does not work. On the other hand, the Java programming language allows all the most useful kinds of expressions in expressions statements, and it does not require a method invocation used as an expression statement to invoke a void method, so such a trick is almost never needed. If a trick is needed, either an assignment statement (§15.26) or a local variable declaration statement (§14.4) can be used instead.

It's not clear exactly why they did things this way, but my guess is that it's intended to reduce bugs. For example, if someone accidentally writes a == b; instead of a=b;, a compiler error will result. In the rare case that someone actually wants this behavior, they have to explicitly assign it to a temp variable (or pass it to a method or similar).

Antimony
  • 37,781
  • 10
  • 100
  • 107