3

This program execution is not getting inside the if condition.
please check the comment i have specified.

public static void filterObject(ArrayList<treewalker.VariableNode> variableNodeslist,
            ArrayList<treewalker.ObjectNode> objectNodeslist,
            ArrayList<treewalker.ClassNode> classNodeslist) {
        int i, j;
        for (i = 0; i < variableNodeslist.size(); i++) {
            for (j = 0; j < classNodeslist.size(); j++) {
                String argu1 = variableNodeslist.get(i).typeClass.toString();
                String argu2 = classNodeslist.get(j).name.toString();
                System.out.println(argu1);
                System.out.println(argu2);
                if (argu1 == argu2)//this if loop is not getting executed 
                {
                    System.out.println("inside for");
                }
                System.out.println("hi");
            }
        }
    }

5 Answers 5

3

Short answer:
argu1.equals(argu2) instead of argu1 == argu2


Longer answer:
The == operator in Java does a reference compare.

You want to do string comparison, use:

if (argu1.equals(argu2))


In some case the == operator might seem to do a equality check for example:

String var1 = "abc";
String var2 = "abc";

String result = ("abc" == "abc");

In this case the result is true which at first look seems to be an equality comparison but is fact a compiler optimisation where both var1 and var2 shares the same reference.

Sign up to request clarification or add additional context in comments.

Comments

1

Use equals

argu1.equals(argu2)

Comments

1

you should be doing if(argu1.equals(argu2)) for string comparision. == will compare the hashcode. For Strings, even thought the 2 strings contain the same text, their hashcodes may differ. Hence you need to use equals() to compare the string text

Comments

1

Use String.equals() to compare your strings, not ==.

Comments

1

Use if(argu1.equals(argu2)) not if(argu1==argu2)

You should never use == to compare strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.