Sachin
Sachin

Reputation: 359

Connection between operator overloading and JVM processing

I need an expert review on this statement. I encountered this statement during my recent java interview.

Adding Operator overloading to java would have definitely made design more complex than without it, and it might have lead to more complex compiler or slows the JVM .

justify this ?

  1. From the above lines i have got 2 questions , how it could slow down the jvm if assume there was operator overloading support in java , because overloading is being resolved during compile time , and jvm is all about run time perspective (correct me if i am wrong at this) .

  2. by making complex compilers we can have more freedom of business logic.

Upvotes: 1

Views: 112

Answers (1)

dave
dave

Reputation: 11975

A couple of thoughts:

(1) Overloading is not always resolvable at compile time. Subclasses may override methods and you do not know whether you have a parent or child class until the code executes. We don't know how operator overloading would have been implemented, but it would probably have the same behaviour.

(2) I don't think this is generally true. Adding operator overloading (essentially syntactic sugar) does not allow you to have more business logic. It may express it more succinctly, but it's the same logic. Consider the following code, where a, b and c are instances of the same class (ie. they're not scalars). The logic is the same, but some will prefer a different expression of it.

c = a.plus(b);
c = a + b;

Upvotes: 2

Related Questions