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?

9
  • I imagine it treats it like any other part of the Java language syntax - it applies a parser, etc. Commented Aug 19, 2016 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. Commented Aug 19, 2016 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. Commented Aug 20, 2016 at 19:36
  • @HelterSkelter the compiler doesn't. Commented Aug 22, 2016 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? Commented Aug 22, 2016 at 22:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, "how the Oracle Java compiler does this exactly" is what interests me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.