News for hackers. Stuff that matters.
Re:Java is not a safe language by nikeee on Monday June 13, 2022 @06:48AM (#31723578)
> 1. Java does not trap overflows

This one struck me some day because this is why Math.abs can return a negative value. When passing Integer.MIN_VALUE, the result will be -2147483648.

There is now StrictMath.absExact that throws an exception in that case.

Re:Java is not a safe language by phoe-krk on Monday June 13, 2022 @07:39AM (#31724025)
That's a known "feature" of most languages that perform modular arithmetic by default, since the negative of Integer.MIN_VALUE is Integer.MIN_VALUE in two's complement arithmetic. Languages that automatically promote to bignums by default (e.g. Lisp) do not suffer from this issue unless modular arithmetic is explicitly requested by the programmer.
Re:Java is not a safe language by nikeee on Monday June 13, 2022 @11:27AM (#31726929)
I've a C# background and was pretty surprised about that. C# throws an exception in that case.
Re:Java is not a safe language by lazulicurio on Monday June 13, 2022 @12:32PM (#31727832)
To be slightly pedantic, C# gives you the option to throw an exception in that case (with either checked blocks or setting the CheckForOverflowUnderflow compiler flag to true), but the default behavior is to allow overflow.
Re:Java is not a safe language by nikeee on Tuesday June 14, 2022 @02:48PM (#31744113)
I was under the impression that they compile with /checked per default. TIL!

Regardless of that flag, Math.Abs will throw an OverflowException: https://referencesource.microsoft.com/#mscorlib/system/math....

Re:Java is not a safe language by lazulicurio on Thursday June 16, 2022 @10:02AM (#31766160)
The default actually depends on project language IIRC. C# has overflow checking disabled by default, VB.net has it enabled by default. Not sure about F#.
Re:Java is not a safe language by phoe-krk on Monday June 13, 2022 @11:50AM (#31727222)
TIL! Thanks!
Re:Java is not a safe language by divs1210 on Monday June 13, 2022 @07:13AM (#31723785)
Wow