Can I assign an operator symbol to a variable and use that variable for a conditional check?
char operator= '>';
int val1=10;
int val2=24;
if(val2 operator val1){
/* some code*/
}
Why cant I use the operator variable inside conditions?
Hey Thats not supported I think this will make sense to me.
The compiler reads in the operator when it builds your app. It has no way of knowing what the operator would be so it cant build correclty which I found in http://www.daniweb.com/software-development/csharp/threads/266385/c-using-operator-as-a-variable-in-calculations
They are talking in the context of C#, but I feel same thing makes sense here as well.
You cannot directly do that, but there are work arounds:
http://www.coderanch.com/t/568212/java/java/arithmetic-operations-operator-stored-variables
If thats really required, we have to use eval sort of thing in our code. I just tried this sample code.
package dumb;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class OperatorAsVariable
{
public static void main( String args[] ) throws ScriptException
{
String test = "+";
System.out.println( 1 + test + 2 );
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName( "js" );
System.out.println( engine.eval( 1 + test + 2 ) );
}
}
Courtesy : Is there an eval() function in Java?
Method arguments in Java must be expressions. And a operator is not an expression. This is not possible in Java.
a better way is pass objects(enums) that represents those operators
example:
public enum Operator{
GREATHERTHAN(">") {
@Override public double apply(double x1, double x2) {
return x1 > x2;
}
},LEESTHAN{
@Override public double apply(double x1, double x2) {
return x1 < x2;
}
}
}
No, you can't do it that way in Java.
To define a binary relation dynamically you need to represent the relation as an object, with a two-argument method to do the check:
if (binaryRelation.areRelated(a,b))
{
// Do something
}
Depending on your needs the standard Comparator
interface may or may not be suitable.
Take a look at the Comparator or Comparable interfaces and how they are used. Then define your own interface which takes two arguments and returns a boolean, and provide different implementations for them (this is OOP).
Another way would be to wait for Java 8 which will have lambda expressions.
Code written in any programming language needs to be converted to Assembly Language. When this happens, every code statement written in High Level language gets translated to instruction / set of instructions in Middle Level equivalent i.e. JAVA code will get translated to Machine Specific instructions in Assembly Language.
Here addition operation in following statement,
int a = a + 10;
May get converted to
ADD A 1010;
And when we try to access a variables value, it may get converted to READ instruction.
So, when you try to use '>' in a variable,
char operator= '>';
int val1=10;
int val2=24;
if(val2 operator val1){
/* some code*/
}
The if statement,
if(val2 operator val1)
will convert to an invalid instruction. Instead of generating an equivalent for comparison of two values, it will READ 'operator' variable.
This will obviously, lead to wrong interpretation.
Hence, doing such thing is not allowed.
Every compiler(java , gcc etc ) may behave differently but the target is same. If you read Complier / Compilation / Execution more, you will know more. These are some links:
http://www.coderanch.com/t/559258/java/java/java-codes-converted-assembly-JVM
Do programming language compilers first translate to assembly or directly to machine code?
Steps for Compilation of A C program.
http://www.herongyang.com/Computer-History/C-Program-Compilation-and-Execution-Process.html