an if statement, maybe not, an expression.. why not?
remember, the expression of an if statement always will turn out to be a boolean.
public static void printResult(boolean b){
if ( b ){
System.out.println("the expression = true");
return;
}
System.out.println("the expression = false");
}
public static void main(String[] args){
printResult(1 <= 2);
printResult(2 <= 1);
}
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
You can pass a String containing any valid JavaScript expression or program and get it evaluated, returning the result to your Java program. This works because JavaScript is a dynamic language, compiled and evaluated at runtime. Requires Java 1.6 or later.
Eg:
ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
try {
String condition = "a==b"; // expression to evaluate
js.put("a", 2); // value for a
js.put("b", 3); // value for b
Object result = js.eval("var a,b;" + condition + ";"); // evaluate expression with values
System.out.println(result);
} catch (ScriptException ex) {
ex.printStackTrace();
}
Sorry about the late reply.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073