<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dave Holoway</title>
    <description>The latest articles on DEV Community by Dave Holoway (@adelphes).</description>
    <link>https://dev.to/adelphes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F413089%2Fce77c2b6-0cb8-4fbe-a9d9-9a288fde9aed.jpeg</url>
      <title>DEV Community: Dave Holoway</title>
      <link>https://dev.to/adelphes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adelphes"/>
    <language>en</language>
    <item>
      <title>Curious case of divide by zero</title>
      <dc:creator>Dave Holoway</dc:creator>
      <pubDate>Sun, 21 Jun 2020 13:09:47 +0000</pubDate>
      <link>https://dev.to/adelphes/curious-case-of-divide-by-zero-3mb4</link>
      <guid>https://dev.to/adelphes/curious-case-of-divide-by-zero-3mb4</guid>
      <description>&lt;p&gt;I'm a fan of diving into the internals of programming languages, compilers and the low-level stuff that most developers (rightly!) ignore but sometimes strangeness lurks in those corners.&lt;/p&gt;

&lt;p&gt;Case in point: Integer division by constant 0 in Java.&lt;/p&gt;

&lt;p&gt;Small background: I'm currently working my way through a project to provide code-completion for Java in VSCode, specifically for Android development. There's long been support for Java in VSCode, but it's not that great and doesn't support some quirks that Android development uses.&lt;/p&gt;

&lt;p&gt;Providing high-quality code-completion means understanding how the underlying programming language works. Not just the syntax, but the rules of engagement that come into play when a project and its source is compiled.&lt;/p&gt;

&lt;p&gt;Back to the post. In almost all programming languages, an integer division by constant zero produces a warning (C, C++) or a compile-time error (C#, go). Unlike floating-point numbers, there's no standard way to represent infinity with integers so it makes sense to ward off developers performing such operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;a.c

int main&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;1/0&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;gcc a.c
a.c: In &lt;span class="k"&gt;function&lt;/span&gt; ‘main’:
a.c:3:11: warning: division by zero &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;-Wdiv-by-zero&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    3 |   &lt;span class="k"&gt;return &lt;/span&gt;1/0&lt;span class="p"&gt;;&lt;/span&gt;
      |           ^
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Enter: &lt;strong&gt;Java.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java, like all languages, likes to optimize at compile-time in order to improve the run-time speed of a program. A really common way to do this is to pre-calculate constant numeric values during compilation. If you were to code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;what gets stored in the compiled class file for field &lt;code&gt;X&lt;/code&gt; is 8. Makes sense, right?&lt;/p&gt;

&lt;p&gt;So, what is happening when this code is compiled - without any warnings or errors from the &lt;code&gt;javac&lt;/code&gt; compiler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;ONE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;INF&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first field can obviously be pre-calculated to 1. But the second field is not representable in JRE integer format - the divide-by-zero will always throw an &lt;code&gt;ArithmeticException&lt;/code&gt; when the class is loaded.&lt;/p&gt;

&lt;p&gt;Now, this is not altogether strange or confusing - the Java language designers just decided to permit all numeric operations during compilation. What &lt;em&gt;is&lt;/em&gt; weird, is that supporting this requires &lt;em&gt;more&lt;/em&gt; work at compile-time to handle a situation which has no benefit to the developer or end-user. I will try to explain...&lt;/p&gt;

&lt;p&gt;When Java source files are compiled, a &lt;strong&gt;class&lt;/strong&gt; file is produced - this is a binary format which contains all the data needed to load and execute methods in the class, including any pre-calculated field values.&lt;br&gt;
Using a decoder, it's possible to look inside the compiled version of the &lt;strong&gt;A&lt;/strong&gt; class above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;mods:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;thisclass:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;superclass:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'java/lang/Object'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;interfaces:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;fields:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;mods:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;name:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'ONE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;type:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'I'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;attributes:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;name:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'ConstantValue'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;info:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;mods:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;name:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'INF'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;type:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'I'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;attributes:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(the above has been edited for brevity - there's a lot more in the class file!)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The bit to notice is that the 'ONE' field has &lt;code&gt;ConstantValue&lt;/code&gt; attribute, but the 'INF' field doesn't. In fact, the 'INF' field is "calculated" in code, as part of a generated class-initialisation routine executed when the class is loaded - during that calculation, the exception is thrown.&lt;/p&gt;

&lt;p&gt;In order to do this, the Java compiler writers need to specifically look out for division-by-zero situations and then code up a completely separate chunk of code in a generated function to throw the inevitable error, stopping the class from ever being loaded at runtime.&lt;/p&gt;

&lt;p&gt;Now Java's raison d'être is to run across any platform with a compatible runtime (remember the "write once, run anywhere" mantra...), so it's perfectly possible that an alternate platform allows infinity-integers (although I can't think of any - let me know if you've seen one)&lt;/p&gt;

&lt;p&gt;I'd have preferred a trivial compile-time&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Error: division by zero.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;but as someone much wiser than me stated: "writing languages is hard".&lt;/p&gt;

</description>
      <category>java</category>
      <category>android</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
