If statement with String comparison fails
==
tests for reference equality.
.equals()
tests for value equality.
==
is for testing whether two strings are the same object.
Comparison symbols is only can use by local variable only.
Public Variable should use "equals" to compare.
Java
@Kroll.method
public void ComparisonTest(String value)
{
String operation = value;
String operation_local = "+";
//get result true
if (operation.equals("+"))
{
System.out.println("equals = true");
}else{
System.out.println("equals = false");
}
//get result false
if (operation == "+")
{
System.out.println("comparison = true");
}else{
System.out.println("comparison = false");
}
//get result true
if (operation_local.equals("+"))
{
System.out.println("local_equals = true");
}else{
System.out.println("local_equals = false");
}
//get result true
if (operation_local == "+")
{
System.out.println("local_comparison = true");
}else{
System.out.println("local_comparison = false");
}
}
JavaScript
Ti.API.info("Input Test = "+calculatorModule.ComparisonTest("+"));
Refer:
http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java