0

As I understand from reading multiple posts here in SO, Java's compiler implements the operators overloading for objects, so that when it sees a simple expression like Integer i = a + b (let a and b be of type Integer, too), it compiles it as a call to the Integer.valueOf() function (as explained here).

I'd like to know how a Java compiler implements this. I mean, does it have a Java C++ [edited according to comments] code underneath that uses a simple operator overloading, so that when seeing a binary + operator with Integer it calls the valueOf() function?

Also, this link appeared in one of the SO answers (sorry, I can't remember where), and I thought maybe the enterBinop part is related to this issue?

Community
  • 1
  • 1
OfirD
  • 9,442
  • 5
  • 47
  • 90
  • I imagine it treats it like any other part of the Java language syntax - it applies a parser, etc. – Oliver Charlesworth Aug 19 '16 at 16:01
  • 2
    You seem to be somewhat confused between _compile_ time and _run_ time. A compiler written completely in Java can rewrite the code it's compiling arbitrarily without any need to have `C` or `C++` or `D` or any other language. The Java compiler (as provided by Oracle's JDK) is **written in Java**. How actual addition is done at runtime is another question entirely, and comes down to JVM bytecode and CPU opcodes. – Boris the Spider Aug 19 '16 at 16:02
  • @BoristheSpider, thank you for making it clear. However, I do interested in knowing the way the compiler implements addition and other operators. – OfirD Aug 20 '16 at 19:36
  • @HelterSkelter the compiler **doesn't**. – Boris the Spider Aug 22 '16 at 18:40
  • @BoristheSpider, who does, then? The compiler is the one who knows that when seeing an expression like `Integer a = 1, b = 3; a = a+b;` it should do some specific operation, and when seeing something like `String s = "he" + "llo";` it should do something else, handling the `+` differently this time. Isn't that operator overloading? – OfirD Aug 22 '16 at 22:05
  • The compiler generates byte code. The JVM interprets byte code to machine code. The CPU runs the machine code. Although both your examples are compile time constant, so the compiler can evaluate them at compile time - but it is still the CPU that does that actual byte manipulation. In the case of your `String` - the `+` would be removed by the compiler. – Boris the Spider Aug 23 '16 at 06:43
  • @BoristheSpider, of course. But the compiler's "decision" that `a+b` is compiled into bytecode x (well, it will compile into several opcodes, but the meaning is clear), while `"he"+"llo"` into bytecode y, is based on its implementation, that if wouldn't have implemented both operators in a different way (=operator overloading) - it couldn't have happen. The way I see it, the fact that the `+` in a `String` expression is removed and replaced by `StringBuilder` etc while in an `Integer` expression it uses unboxing and/or autoboxing, indicates an operator overloading by the compiler. Isn't it? – OfirD Aug 23 '16 at 08:14
  • In your example, the `+` in the `String` would simply be elided; it would be compile to `String s = "hello"`. Operator overloading doesn't _really_ exist in Java in any real sense. `+` is rtl associative, if the LHS is a `String` is simply becomes `l.concat(Objects.toString(r))`; the compiler is allowed to replace `String.concat` with a `StringBuilder` as an optimisation. I don't really understand what your question is getting at tbh. – Boris the Spider Aug 23 '16 at 09:40
  • @BoristheSpider, ok, then, I guess I'll just have to accept that operator overloading in Java simply doesn't exist in any kind of form similiar to this in C++ (It confused me because there are some SO answers that says it exists in Java, but only within the compiler). Thanks. – OfirD Aug 23 '16 at 09:50

1 Answers1

2

The Java compiler doesn't allow operator overloading, unlike C++.

There are builtin overloaded operators, though, for instance "+" for adding numbers or concatenating strings.

The reason you can add objects of type Integer in recent versions of Java is that the compiler will perform auto-boxing and unboxing of objects related to primitive types, like Integer->int, Boolean->boolean and so on.

So when you write "a + b", the compiler will see that the variables have type Integer and unbox them to type int automatically. (This is simplified to make it understandable. I don't know how the Oracle Java compiler does this exactly.)

Note that this can and will throw a NullPointerException if any of those Integer variables are null, because the code compiles to a.intValue() + b.intValue(), like you said.