I noticed something weird earlier today. I was writing some code that was supposed to make graphs in complex quadrants. Anyway, I typed int i = 1/0; and it wouldn't compile. When I changed the code to double i = 1.0/0.0; the code compiled fine. When I ran the code it gave an error / by 0. I was expecting that... But why does it compile fine when using doubles and not integers? I am using the Blue J IDE
            Asked
            
        
        
            Active
            
        
            Viewed 157 times
        
    3
            
            
         
    
    
        ChriskOlson
        
- 513
- 1
- 3
- 12
- 
                    1They both compile fine for me, and the int version throws an error, which is expected behavior as far as I know. Are you sure you haven't gotten them mixed up in your head? – Vitruvie Feb 09 '14 at 20:10
- 
                    I think much better explanation can be found here: http://stackoverflow.com/questions/2381544/why-doesnt-java-throw-an-exception-when-dividing-by-0-0 – Kuba Spatny Feb 09 '14 at 20:11
- 
                    Also a third question: http://stackoverflow.com/questions/21380499/is-this-declaration-possible/21380590 – Radiodef Feb 09 '14 at 20:14
1 Answers
4
            Dividing an int value by zero would result in a ArithmeticException, hence the expression 1 / 0 is illegal.
The result of dividing a double value by zero is infinity or NaN *, so the expression 1.0 / 0.0 is legal.
*) See t_over's comment for specifics:
 
    
    
        Tony the Pony
        
- 40,327
- 71
- 187
- 281
- 
                    1Just to add.. Dividing a negative number by 0.0 equals negative infinity, and dividing zero by zero results in a Double.NaN – t_over Feb 09 '14 at 20:15
-