Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Can Java 8 do exact arithmetic as in C# Date: Mon, 8 Jan 2018 20:04:56 -0500 Organization: Aioe.org NNTP Server Lines: 49 Message-ID: References: <0ef8b3c0-e45c-4624-a8b8-2bdc67135380@googlegroups.com> NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 X-Notice: Filtered by postfilter v. 0.8.2 Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:37944 On 1/8/2018 7:17 AM, bursejan@gmail.com wrote: > Java 8 has a nice feature, maybe this > isn't known very well. But there are > > a couple of new operations in Math: > > /** > * Returns the sum of its arguments, > * throwing an exception if the result overflows an {@code int}. > * > * @param x the first value > * @param y the second value > * @return the result > * @throws ArithmeticException if the result overflows an int > * @since 1.8 > */ > public static int addExact(int x, int y); > > /** > * Returns the sum of its arguments, > * throwing an exception if the result overflows a {@code long}. > * > * @param x the first value > * @param y the second value > * @return the result > * @throws ArithmeticException if the result overflows a long > * @since 1.8 > */ > public static long addExact(long x, long y); Nice. But probably of limited value. The programmers calling these methods do so because they are aware of the risk of overflow. If these methods did not exist they would still be aware and (hopefully) do something else to handle the problem. The problem is the programmers not aware of the risk of overflow. And these methods does not help them. Arne