Dynamic issue with operator evaluation with string matching

I tried the example provided in this thread to dynamically create an if statement using BeanShell . But it does not work fine. Instead of using the "age" variable as an integer, I used the string in the example below. I get "failure" as the answer instead of "success".

Can anybody help me?

/* To change this template, choose Tools | Templates and open the template in the editor. */ import java.lang.reflect.*; import bsh.Interpreter; public class Main { public static String d; public static void main(String args[]) { try { String age = "30"; String cond = "age==30"; Interpreter i = new Interpreter(); i.set("age", age); System.out.println(" sss" + i.get("age")); if((Boolean)i.eval(cond)) { System.out.println("success"); } else { System.out.println("fail"); } } catch (Throwable e) { System.err.println(e); } } } 

Thanks Mani

+1
source share
5 answers

You need to choose either a numerical comparison or a string comparison. This requires the use of a compatible condition and type for age.

Numerical:

  int age = 30; String cond = "age==30"; 

String

  String age = "30"; String cond = "age.equals(\"30\")"; 
+1
source

When you compare two objects with the == operator, you are comparing two links. You are essentially asking whether two different names refer to the same object in memory.

To compare the actual values โ€‹โ€‹of the objects, you need to use the equals() method. This is what is very important for understanding Java.

0
source

You use == to compare string types. Use age.equals("30") instead.

EDIT : show his work

If you use this as a cond definition:

 String cond = "age.equals(\"30\")"; 

Output:

  sss30 success 

In response to a question about using =="30" instead, here is the answer to this question:

If your String age interned because it is a compile-time constant, then this may be true.

 final String age = "30"; 

However, if you are explicitly a new String , otherwise it is not interned, then it will be false.

 String age = new String("30"); 

You can run both examples to see this in action. Perhaps you can get fail for both.

Now, just because internment exists does not mean that it should rely on it to compare String types. The == operator should only be used to compare primitives with each other and to compare reference types to see if they point to the same object, so for reference types we can say that it sees if two objects are identical instead of equal .

Sometimes, through the magic of the JVM and JDK, String and other primitive wrappers, such as Integer , may be comparable to == , but situations for this are limited and unreliable.

0
source

@Matthew Flaschen is right. Aside, you can simplify your conclusion as follows:

 System.out.println(cond + " is " + i.eval(cond)); 

which produces

age == 30 true

0
source

Comparing strings with "==" in the bsh interpreter does not work as expected.

It works as follows: (copied from the link below)

  Beanshell handles '==' like java and not eg like JavaScript.  That means you've got to use "equals" to compare instances.
     bsh% "abc" == "abc";  => true
     bsh% "abc" == new String ("abc");  => false
     bsh% "abc" .equals ("abc");  => true
     bsh% "abc" .equals (new String ("abc"));  => true

more information here: https://code.google.com/p/beanshell2/issues/detail?id=86

So you have to use ".equal ()" or compile your own version of bsh, as I did. (read the full release above)

0
source

All Articles