Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!15g2000vbw.googlegroups.com!not-for-mail From: Tom McGlynn Newsgroups: comp.lang.java.programmer Subject: Re: Arithmetic overflow checking Date: Thu, 21 Jul 2011 07:15:59 -0700 (PDT) Organization: http://groups.google.com Lines: 34 Message-ID: <208c3e0b-4e3e-4180-97ae-53db9b8ec2e9@15g2000vbw.googlegroups.com> References: <015aeb15-57db-48ab-9cd4-77f8448b632f@w24g2000yqw.googlegroups.com> <1f9c17dltrhlmhifuigoa914477r4rg1e1@4ax.com> <09fe171s46ilvq9qmn254dctunm6noh0ps@4ax.com> <4e262731$0$314$14726298@news.sunsite.dk> <4e26300b$0$309$14726298@news.sunsite.dk> <4e26b4ed$0$2501$db0fefd9@news.zen.co.uk> <4e28097f$0$2533$da0feed9@news.zen.co.uk> NNTP-Posting-Host: 128.154.208.243 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1311257855 25469 127.0.0.1 (21 Jul 2011 14:17:35 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 21 Jul 2011 14:17:35 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 15g2000vbw.googlegroups.com; posting-host=128.154.208.243; posting-account=uCwGCAkAAADsLxg6F1MzUXy8bLSglYNp User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HUALESNKRC X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6340 On Jul 21, 8:43=A0am, Martin Gregorie wrote: ... > Agreed, but there's another way to handle this in Java that would > probably cater for most overflow and out of range requirements without > affecting existing programs or the language at all: > > Just add range checking constructors to Integer and friends, e.g: > > =A0 =A0 =A0 =A0 Integer boundedValue =3D new Integer(0,1,10); > > to declare an Integer, initialised to zero and with the range 1:10. The > overheads would be minimal if the checks were only enabled if limits were > added. It could either throw an unchecked exception or provide a range > checking method: > How would this help given that Integers can't change anyway? It can only check that the initialization value is in range. You'd have to somehow make these Integers 'sticky' in some way so that any expression using one of them is also subject to bounds checking. E.g., Integer firstValue =3D new Integer(Integer.MAX_VALUE, 0, Integer.MAX_VALUE); Integer nextValue =3D new Integer(firstValue+Integer.MAX_VALUE+3, 0, 10); clearly overflows, but it would work fine unless something significant were added to the language. Regards, Tom McGlynn