title: Why doesn't the ArrayList contains() method work with objects?
body: <p>I'm learning Java and run into a problem.  Why doesn't the ArrayList contains() method work with objects?  It seems to work with Strings and other basic data types but not objects.  Am I using the wrong method?</p>
  
  <p>The program creates data in lines 8 - 11 and receives it from the command line.  The data from the command line is:</p>
  
  <pre><code>avacado 10 .50 broccoli 20 .75 cauliflower 30 1.00 banana 300 .55
  </code></pre>
  
  <p>The last entry "banana 300 .55" on the command line matches the data on line 11 and should be bypassed as already existing but contains() says it's not there.  How come?  Help.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>package java21days;
  
  import java.util.*;
  
  public class FruitBasket3 {
  
      ArrayList&lt;Fruit&gt; cart;
      Fruit apple = new Fruit("apple", 100, .75F);
      Fruit orange = new Fruit("orange", 200, .90F);
      Fruit banana = new Fruit("banana", 300, .55F);
      Fruit kumkwat = new Fruit();
  
      public FruitBasket3(String[] userCodes) {
          cart = new ArrayList&lt;&gt;();
          int quantity;
          float price;
  
          addCode(apple);
          addCode(orange);
          addCode(banana);
          addCode(kumkwat);
  
          for (int j = 0; j &lt; userCodes.length; j+=3) {
              quantity = Integer.parseInt(userCodes[j+1]);
              price = Float.parseFloat(userCodes[j+2]);
              addCode(new Fruit(userCodes[j],quantity,price));
          }
  
          // display all codes
          for (Fruit code : cart) {
              System.out.println("Fruit: " + code.getName() + " Price: " +
                      code.getQuantity() * code.getPrice());
          }
      }
  
      private void addCode(Fruit inFruit) {
          boolean match = false;
          System.out.println("Inside addCode with fruit: " + inFruit.getName() + 
                  " Quantity: " + inFruit.getQuantity() + " Price: " + inFruit.getPrice());
          if (!cart.contains(inFruit)) {
              System.out.println("    Passed the contains test.");
              for (Fruit item : cart) {
                  System.out.println("    Existing: " + item.getName() + 
                          " Quantity: " + item.getQuantity() + " Price: " + item.getPrice());
                  System.out.println("    New:      " + inFruit.getName() + 
                          " Quantity: " + inFruit.getQuantity() + " Price: " + inFruit.getPrice());
  
                  //if (inFruit.getName() == item.getName() &amp;&amp;
                  //        inFruit.getQuantity() == item.getQuantity() &amp;&amp;
                  //        inFruit.getPrice() == item.getPrice()){
                  if (inFruit.getName().equals(item.getName()) &amp;&amp;
                          inFruit.getQuantity() == item.getQuantity() &amp;&amp;
                          inFruit.getPrice() == item.getPrice()){
                      match = true;
                      System.out.println("        Did not pass the individual element comparision test.");
                  } else {
                      System.out.println("        Passed the individual element comparision test.");
                  }
              }
              if (!match) {
                  System.out.println("            &gt;&gt;&gt;&gt; adding.");
                  cart.add(inFruit);
              } else {
                  System.out.println("            &gt;&gt;&gt;&gt; did not add.");
              }
          } else {
              System.out.println("    Fruit: " + inFruit.getName() + " already on file ... bypassing.");
          }
      }
  
      public static void main(String[] arguments) {
          if (arguments.length % 3 != 0) {
              System.out.println("Error!!! Incorrect number of arguments.");
          } else {
              FruitBasket3 keeper = new FruitBasket3(arguments);
          }
      }
  
  }
  
  public class Fruit {
      private String name;
      private int quantity;
      private float price;
  
      public Fruit() {
          name = "";
          quantity = 0;
          price = 0.0F;
      }
  
      public Fruit(String inName, int inQuantity, float inPrice) {
          name = inName;
          quantity = inQuantity;
          price = inPrice;
      }
  
      public String getName(){
          return name;
      }
  
      public int getQuantity(){
          return quantity;
      }
  
      public float getPrice(){
          return price;
      }
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>run:
  Inside addCode with fruit: apple Quantity: 100 Price: 0.75
      Passed the contains test.
              &gt;&gt;&gt;&gt; adding.
  Inside addCode with fruit: orange Quantity: 200 Price: 0.9
      Passed the contains test.
      Existing: apple Quantity: 100 Price: 0.75
      New:      orange Quantity: 200 Price: 0.9
          Passed the individual element comparision test.
              &gt;&gt;&gt;&gt; adding.
  Inside addCode with fruit: banana Quantity: 300 Price: 0.55
      Passed the contains test.
      Existing: apple Quantity: 100 Price: 0.75
      New:      banana Quantity: 300 Price: 0.55
          Passed the individual element comparision test.
      Existing: orange Quantity: 200 Price: 0.9
      New:      banana Quantity: 300 Price: 0.55
          Passed the individual element comparision test.
              &gt;&gt;&gt;&gt; adding.
  Inside addCode with fruit:  Quantity: 0 Price: 0.0
      Passed the contains test.
      Existing: apple Quantity: 100 Price: 0.75
      New:       Quantity: 0 Price: 0.0
          Passed the individual element comparision test.
      Existing: orange Quantity: 200 Price: 0.9
      New:       Quantity: 0 Price: 0.0
          Passed the individual element comparision test.
      Existing: banana Quantity: 300 Price: 0.55
      New:       Quantity: 0 Price: 0.0
          Passed the individual element comparision test.
              &gt;&gt;&gt;&gt; adding.
  Inside addCode with fruit: avacado Quantity: 10 Price: 0.5
      Passed the contains test.
      Existing: apple Quantity: 100 Price: 0.75
      New:      avacado Quantity: 10 Price: 0.5
          Passed the individual element comparision test.
      Existing: orange Quantity: 200 Price: 0.9
      New:      avacado Quantity: 10 Price: 0.5
          Passed the individual element comparision test.
      Existing: banana Quantity: 300 Price: 0.55
      New:      avacado Quantity: 10 Price: 0.5
          Passed the individual element comparision test.
      Existing:  Quantity: 0 Price: 0.0
      New:      avacado Quantity: 10 Price: 0.5
          Passed the individual element comparision test.
              &gt;&gt;&gt;&gt; adding.
  Inside addCode with fruit: broccoli Quantity: 20 Price: 0.75
      Passed the contains test.
      Existing: apple Quantity: 100 Price: 0.75
      New:      broccoli Quantity: 20 Price: 0.75
          Passed the individual element comparision test.
      Existing: orange Quantity: 200 Price: 0.9
      New:      broccoli Quantity: 20 Price: 0.75
          Passed the individual element comparision test.
      Existing: banana Quantity: 300 Price: 0.55
      New:      broccoli Quantity: 20 Price: 0.75
          Passed the individual element comparision test.
      Existing:  Quantity: 0 Price: 0.0
      New:      broccoli Quantity: 20 Price: 0.75
          Passed the individual element comparision test.
      Existing: avacado Quantity: 10 Price: 0.5
      New:      broccoli Quantity: 20 Price: 0.75
          Passed the individual element comparision test.
              &gt;&gt;&gt;&gt; adding.
  Inside addCode with fruit: cauliflower Quantity: 30 Price: 1.0
      Passed the contains test.
      Existing: apple Quantity: 100 Price: 0.75
      New:      cauliflower Quantity: 30 Price: 1.0
          Passed the individual element comparision test.
      Existing: orange Quantity: 200 Price: 0.9
      New:      cauliflower Quantity: 30 Price: 1.0
          Passed the individual element comparision test.
      Existing: banana Quantity: 300 Price: 0.55
      New:      cauliflower Quantity: 30 Price: 1.0
          Passed the individual element comparision test.
      Existing:  Quantity: 0 Price: 0.0
      New:      cauliflower Quantity: 30 Price: 1.0
          Passed the individual element comparision test.
      Existing: avacado Quantity: 10 Price: 0.5
      New:      cauliflower Quantity: 30 Price: 1.0
          Passed the individual element comparision test.
      Existing: broccoli Quantity: 20 Price: 0.75
      New:      cauliflower Quantity: 30 Price: 1.0
          Passed the individual element comparision test.
              &gt;&gt;&gt;&gt; adding.
  Inside addCode with fruit: banana Quantity: 300 Price: 0.55
      Passed the contains test.
      Existing: apple Quantity: 100 Price: 0.75
      New:      banana Quantity: 300 Price: 0.55
          Passed the individual element comparision test.
      Existing: orange Quantity: 200 Price: 0.9
      New:      banana Quantity: 300 Price: 0.55
          Passed the individual element comparision test.
      Existing: banana Quantity: 300 Price: 0.55
      New:      banana Quantity: 300 Price: 0.55
          Did not pass the individual element comparision test.
      Existing:  Quantity: 0 Price: 0.0
      New:      banana Quantity: 300 Price: 0.55
          Passed the individual element comparision test.
      Existing: avacado Quantity: 10 Price: 0.5
      New:      banana Quantity: 300 Price: 0.55
          Passed the individual element comparision test.
      Existing: broccoli Quantity: 20 Price: 0.75
      New:      banana Quantity: 300 Price: 0.55
          Passed the individual element comparision test.
      Existing: cauliflower Quantity: 30 Price: 1.0
      New:      banana Quantity: 300 Price: 0.55
          Passed the individual element comparision test.
              &gt;&gt;&gt;&gt; did not add.***
  Fruit: apple Price: 75.0
  Fruit: orange Price: 180.0
  Fruit: banana Price: 165.0
  Fruit:  Price: 0.0
  Fruit: avacado Price: 5.0
  Fruit: broccoli Price: 15.0
  Fruit: cauliflower Price: 30.0
  BUILD SUCCESSFUL (total time: 0 seconds)
  </code></pre>