views:

79

answers:

4

What is the difference in the evaluation rule of an operator and a method in Java?

+2  A: 

Operators can be seen as syntactic sugar for

static Foo Plus (Foo right, Foo left)
 {
   // do stuff
   return output;
 }

It's just more convinient to write right + left instead of Class.Plus(right, left).

Femaref
But not in C or Java.
SLaks
A: 

In both C and Java you cannot override '+' operator for your classes so your question makes sense for other languages.

There are languages where operators can be overriden such as C++ and Python. If you are interested in such comparison why not to try? Even of your tests shows that '+' operator is slower/faster than .add() method this can change if you change compiler or environment.

I think you should use something that looks good for you.

There was advices in Java to use StringBuffer instead of + operator with String but newest Java implementations make better optimalization for + operator, so this advice is not that good for new code.

Michał Niklas
A: 

Femaref's answer is pretty good; I'd like to expand on it.

Operators are (usually) hard-wired into the language: Stuff like +, -, * and / are usually directly translated by the compiler into machine language (if that's the underlying mechanism for that language system) without the need to explicitly call a method in a library. This is how it is/was in C, for instance. If those operators weren't defined in the language, you'd have to code plus(2,2) instead of 2 + 2.

Operators defined in the language come with the benefit of built-in priority. * and / usually have higher priority than + and -, so you can write 3 * 3 + 4 * 4 and get 25, not 52 or 84 that you'd get without such priorization or different priorities.

The line becomes a little grey when operators are recognized by the compiler but still delegated to a library. Complex numbers in FORTRAN may be an example: Often the compiler will not bother to directly compile complex operations into machine code but will generate machine code to call a library.

Most people think only of the arithmetic operators, like +, - and so forth. But you could also consider square brackets ( [ ] ) an operator for array indexing, for example.

Some languages let you overload their operators, so you can substitute a call to a method for whatever the operator normally does.

Most languages don't let you define your own operators that support the same mechanisms as the built-in ones. Scala is an exception here; you can define a +++ operator if you like and have it hook up to a method you provide. Some people think operator overloading makes code harder to understand, so the jury is still out about whether this is a good idea.

Carl Smotricz
A: 

I'll take a different guess at what you're asking. You might be asking, for instance, what Java guarantees about the order in which bar() and baz() are executed in the following two cases:

foo.bar() + foo.baz()
fee(foo.bar(), foo.baz())

In both cases, bar() is definitely called before baz() (JLS 15.7.1,4)

Sean Owen