<?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: Neema Rajasree Soman</title>
    <description>The latest articles on DEV Community by Neema Rajasree Soman (@rfdevuraja).</description>
    <link>https://dev.to/rfdevuraja</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%2F934586%2Fc2267fd4-dfcb-473c-9c60-fcc0b35aee05.png</url>
      <title>DEV Community: Neema Rajasree Soman</title>
      <link>https://dev.to/rfdevuraja</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rfdevuraja"/>
    <language>en</language>
    <item>
      <title>Divide By Zero</title>
      <dc:creator>Neema Rajasree Soman</dc:creator>
      <pubDate>Tue, 11 Oct 2022 18:12:36 +0000</pubDate>
      <link>https://dev.to/rfdevuraja/divide-by-zero-378a</link>
      <guid>https://dev.to/rfdevuraja/divide-by-zero-378a</guid>
      <description>&lt;p&gt;Mathematically, any number when divided by zero is not defined. However in Java programming, the division by zero usually gets related to an Exception, which is &lt;em&gt;ArithmeticException&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class DivisionByZero {

   public static void main(String[] args){
      System.out.println( 5 / 0 );
   }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet gives us a &lt;em&gt;ArithmeticException&lt;/em&gt; as below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Exception in thread "main" java.lang.ArithmeticException: divide by zero&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This could lead most of us to the assumption that whenever we divide a number by zero in Java we get the &lt;em&gt;ArithmeticException&lt;/em&gt;, but that isn't the case.&lt;/p&gt;

&lt;p&gt;What if the datatype of the number being divided by zero is float or double?&lt;/p&gt;

&lt;p&gt;Let's check out,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class DivisionByZero {

   public static void main(String[] args){
      System.out.println( 5.0f / 0);
      System.out.println( 6.0d / 0);
      System.out.println( 0f / 0);
      System.out.println( -7.0f / 0);
   }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the above code gets executed no exception will be thrown, rather we get the below output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Infinity
Infinity
NaN
-Infinity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So why didn't the division by zero with float or double values didn't throw any exception.&lt;/p&gt;

&lt;p&gt;It's because, in Java division of an integer by 0 is not covered  by the IEEE 754 standards therefore generates an &lt;em&gt;ArithmeticException&lt;/em&gt;. In case of division of float or double values Java follows the IEEE 754 standards and have special numeric values that represent the result of such operations such as &lt;code&gt;NaN, Infinity &amp;amp; -Infinity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So to summarize, In Java values such as &lt;code&gt;Nan &amp;amp; Infinity&lt;/code&gt; are available only for floating-point numbers ( As per the IEEE 754 standards), therefore the division by zero operation on floating-point numbers is allowed. Since no special values like &lt;code&gt;Nan&lt;/code&gt; are aligned to integer, the division by zero results in &lt;em&gt;ArithmeticException&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Hope this article is useful. Thanks!&lt;/p&gt;

</description>
      <category>java</category>
      <category>exception</category>
    </item>
  </channel>
</rss>
