Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!feeder2.news.elisa.fi!uutiset.elisa.fi!7564ea0f!not-for-mail Newsgroups: alt.comp.lang.borland-delphi,comp.lang.c,comp.lang.java.programmer From: Heikki Kallasjoki Subject: Re: Oracle/Google demonstrate human beings cannot write 10 lines of code without making a mistake ;) References: <29308868.1994.1337265697084.JavaMail.geo-discussion-forums@pbcuc6> <84131$4fb54067$5419acc3$20839@cache90.multikabel.net> Organization: nonexistent Followup-To: comp.lang.java.programmer User-Agent: slrn/pre1.0.0-18 (Linux) Message-ID: Lines: 39 Date: Thu, 17 May 2012 19:13:00 GMT NNTP-Posting-Host: 91.156.75.88 X-Complaints-To: newsmaster@saunalahti.com X-Trace: uutiset.elisa.fi 1337281980 91.156.75.88 (Thu, 17 May 2012 22:13:00 EEST) NNTP-Posting-Date: Thu, 17 May 2012 22:13:00 EEST Xref: csiph.com comp.lang.c:20926 comp.lang.java.programmer:14591 On 2012-05-17, Skybuck Flying wrote: >>> private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) >>> { >>> if (fromIndex > toIndex) >>> throw new IllegalArgumentException("fromIndex(" + fromIndex + >>> ") > toIndex(" + toIndex+")"); >>> if (fromIndex < 0) >>> throw new ArrayIndexOutOfBoundsException(fromIndex); >>> if (toIndex > arrayLen) >>> throw new ArrayIndexOutOfBoundsException(toIndex); >>> } > > It seems likely this code is called right before the array is accessed to > try and prevent an access violation, instead it tries to throw a somewhat > nicer exception message to indicate a problem with array access. No. It seems likely rangeCheck(10, 4, 7) is testing whether indices 4, 5 and 6 (that is the *range* from 4, inclusive, to 7, exclusive) are within the bounds of an array of length 10, which it does just fine. > It's simply not valid. Out of bounds has a very clear meaning in programming > practice. It's either within bounds or it's not. The bounds of a java array Yes, but it is not designed for testing whether fromIndex and toIndex are within those bounds. It is used to test whether fromIndex, fromIndex+1, ..., toIndex-1 are, which it does. In particular, the element with index "toIndex" is not part of the range it's testing, so it does not need to be inside the array bounds. In other words, the range fromIndex=5, toIndex=10 denotes the elements 5, 6, 7, 8 and 9. It does not include element 10. rangeCheck(10, 5, 10) is not supposed to throw an exception, nor does it do so. (Would it be possible to also stop crossposting to all the unrelated groups?) -- Heikki Kallasjoki