Hi!

So, my question is how could I compare two objects. The code is shown below. The problem is that the first "if" statement is always true, even if it should be false. For instance, let's say childHierarchy and hierarchy[0] are equal to 'Contract'. In this case "if (childHierarchy.toString() != hierarchy[0].toString())" returns true. But why? Thanks.


Vector formTypes = new Vector();
formTypes = SystClasses.Folder.returnDataFromSelectQuery("select of_title from OrgFolders");
Object hierarchy[] = formTypes.toArray();
...
Vector childFormTypes = new Vector();
childFormTypes = SystClasses.Folder.returnDataFromSelectQuery("select of_childOf from OrgFolders");
Object childHierarchy[] = childFormTypes.toArray();
...
      if (childHierarchy[i].toString() != hierarchy[0].toString()) // Ie node with children
       {
          System.out.println(childHierarchy[i].toString() + ", " + hierarchy[0].toString());         
       } else { // Ie Leaf
          Object nodeSpecifier = hierarchy[i];
          child = new DefaultMutableTreeNode(nodeSpecifier);
          node.add(child);
       }

ok, it was easy. I just had to use "equals" :)

You need to look at what toString() of Object return. Also, you should use equals() for object comparison instead of attempt to compare using reference comparison symbol. You may need to read more about equals() in Java because it is not an easy topic. Furthermore, there are restriction and problem with class inheritance as you are attempting to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.