title: Getting the reciprocal in a number exception in Java
body: <p>I am creating a Java swing calculator. I want to get the reciprocal of a given number. So if I input 0, it should print out an error message. </p>
  
  <p>Here's my code:</p>
  
  <pre><code>public class Calculator extends JFrame implements ActionListener {
      double num=0, num2=0;
      String operator;
      JButton bReciprocal=new JButton("1/x");
      JTextField result=new JTextField("0", 25);
  
      public void actionPerformed(ActionEvent e) {
          String command=e.getActionCommand();
          if(command=="1/x") {
              try {
                  num=1/num;
                  result.setText(Double.toString(num));
              }
              catch(ArithmeticException ae) {
                  result.setText("Math Error");
                  num=0;
              }
          }
      }
  }
  </code></pre>
  
  <p>However, if I give <code>0</code> as the input, what I get is <code>infinity</code>. What's wrong with this code? How can I make it show "Math error" instead of <code>infinity</code>?</p>