What will be the output of the following Java code?

class Output
      public static void main(String args[])
          Double y = new Double(257.57812);
      Double i = new Double(257.578123456789);  
          int x = i.compareTo(y);
          catch(ClassCastException e)

a) 0
b) 1
c) Exception
d) None of the mentioned

1 thought on “What will be the output of the following Java code?”

  1. b
    Explanation: i.compareTo() methods two double values, if they are equal then 0 is returned and if not equal then 1 is returned, here 257.57812 and 257.578123456789 are not equal hence 1 is returned and stored in x.
    $ javac Output.java
    $ java Output

Leave a Comment