Is there a way to use char in an arithmetic operation in Java?

3

The following code does not work, but I thought of something like this:

char op = '*';
...
r = (y op x);

I want to change the characters of op to do different operations, is this possible in any other way?

    
asked by anonymous 30.08.2015 / 20:38

2 answers

5

In the language does not have. Only languages that allow execution of arbitrary codes dynamically allow this, and generally in a different way.

There is also no reason to have this in a language. If you need something like this you should create a code that treats it and performs the operations you want. There are countless ways to do this, but that's not what the question asks. Whatever she does, there's no way.

In general you will have to create complex codes to get the job done correctly. None will facilitate the coding work if this is the goal. And the code will certainly be less readable. It is a very bad idea to allow this directly.

    
30.08.2015 / 20:44
1

The ideal in this case would be the use of a conditional case, so yes you could use characters for your case follows the example:

    switch (op) {
      case '+': 
             r = primeiro + segundo;
             break;
      case '-': 
            r = primeiro - segundo;
            break;
      case '*': 
            r = primeiro * segundo;
            break;
     ....
    } 
    
01.09.2015 / 14:37