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);

Edited by LianaN: n/a

7 Years
Discussion Span
Last Post by Taywin

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.