2

I have spent hours researching this, and I'm still very confused.

I, like many others, searched this on Google and found this SO thread.

However, recently, a student in one of my programming courses told me that this wasn't true, and in fact, the instructor verified it. Hell, the entire class heard it, and no one contested him. He offered this link from Java.net as proof.

I said to him immediately, "That looks like JavaScript," but he pointed to this section:

class User {
  List roles;

  User() {
    roles = new List();
  }

  operator +(Role newRole) {  
    this.roles.add(newRole); 
  }  
}

main() {
  User alice = new User();
  Role adminUser = new Role("TIMESHEET_ADMIN", 3);

  alice + adminUser; 
  print(alice.roles.length); 
}

and told me that it was indeed Java. I, having no experience in JS couldn't tell, and he got away (for now), but I tried putting this into my IDE (IntelliJ), and I was assaulted by compiler errors. I didn't even know where to begin with fixing them, but the main thing was the cannot resolve symbol "operator".

I'm not looking here for any discussion on Java's built-in operator overloading, I am looking for a definitive answer on whether or not users in Java can overload operators themselves.

Community
  • 1
  • 1
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
  • 1
    The java.net link doesn't go to java.net – AntonH Jan 29 '14 at 01:03
  • 3
    Looks more like C#, but I'm no expert with that. The main issue I would have, part from getting it to actually compile, would be the use of `List`, `main` and `print`. Java doesn't have `List` class in the Collections API, it has an interface, which would require the `length` method to look like `length()` – MadProgrammer Jan 29 '14 at 01:03
  • I looked at Java.net, and the only article about operator overloading was talking about Dart, not Java (except to say that it's a shame that Java doesn't do operator overloading). Also, the code posted isn't Java. – AntonH Jan 29 '14 at 01:07
  • I don't think JavaScript supports operator overloading either. – ajb Jan 29 '14 at 01:08
  • 1
    @MadProgrammer I checked [here](https://weblogs.java.net/blog/manningpubs/archive/2013/01/11/operator-overloading), and it's Dart. It's mentionned in the summary article and throughout the article. Don't know how it would be confused with Java. – AntonH Jan 29 '14 at 01:12
  • Even without resorting to the jls (which I agree is the ultimate source of truth), it's pretty easy to try to compile this with javac and see that it doesn't compile. – yshavit Jan 29 '14 at 01:19
  • AHH https://weblogs.java.net/blog/manningpubs/archive/2013/01/11/operator-overloading I meant this sorry – michaelsnowden Jan 29 '14 at 01:20
  • The article mentions that it's about the Dart programming language and does so prominently. I still don't see why this question was even asked as trivial research would have answered it. – Hovercraft Full Of Eels Jan 29 '14 at 01:51
  • This looks like JScript.NET, javascript for .net. (EDIT: The article mentioned that it's the Dart language) – Sri Harsha Chilakapati Sep 03 '14 at 10:23

1 Answers1

12

The definitive answer is no, Java does not support operator overloading except for with String types. But more importantly, a larger answer to your question is that you should look in the one single document, the Java Language Specification or JLS that will answer this and any other similar question about what Java does or doesn't contain. Don't debate with anyone about maybes or ifs. Go look for yourself and satisfy for yourself that there is no such thing as operator overloading for Java except for the magical String type. It's all there in the JLS, and that's what it's for.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373