Wednesday, November 30, 2005

Divide By Zero in Java

After developing for so long in Java, today only I realize that we can actually do divide by zero in Java and does not get runtime exception. I think so far, this is the only language I can see executing divide by zero and show infinity as a result. Most runtime will throw error whenever it encounter divides by zero. This shows that I still lack a lot of basic Java knowledge. Must improve myself more and not let my skills be rusty. To allow divide by zero, zero literal you use should be in double.
public class TestDivideByZero{
    public static void main(String[] args){     
        System.out.println("This will show as infinity : " + 100/0.0);
        System.out.println("The following will throw runtime exception : ");
        System.out.println("Exception :" + 100/0);

1 comment:

  1. A bit late to answer… but anyway another language that shows infinitiy when you divide by 0 is the R programming language

    And you don't even need to care about integers or float.