Reputation: 1088
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");
}
}
}
Upvotes: 3
Views: 429
Reputation: 116837
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.
Upvotes: 3
Reputation: 4785
Use if(argu1.equals(argu2))
not if(argu1==argu2)
You should never use ==
to compare strings.
Upvotes: 1
Reputation: 986
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
Upvotes: 1