• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Sybex OCP11 - Chapter 1 - Possibly Inaccurate Statement of Operator Overloading

 
Rancher
Posts: 129
15
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first chapter of the books (1z0-815 and complete version) mentioned several times that java got rid of operator overloading.

Actually, Java didn't get rid of operator overloading completely. E.g., the plus sign "+" is overloaded for string concatenation, and "&" is overloaded for boolean and integral types.

It may be better to say that java doesn't support "user defined operator overloading", because java is still using the technique, but users are not allowed to do so.

Is it the so called "One man may steal a horse, while another may not look over a hedge?"
 
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book is accurate.  You’re mixing “supporting operators” with “operator overloading”.  Java does support a number of operators, such as + for addition and string concatenation.  It does not support operator overloading, though, as it is traditionally defined.  To put it another way, when someone says “language X supports operator overloading”, it implicitly mean what you call “user defined operator overloading”.  That you can customize operators to have different meanings on different classes.

For example, if class Z is a regular class (not a number or string), and you have two instances z1 and z2, you cannot define what z1 + z2 is.
 
Master Rancher
Posts: 4225
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a bit confusing because Java chose to support the + operator in a way that was similar to what was done in C++ with operator overloading.  But that doesn't mean it's actually overloading in Java - just that it was similar to something else in C++.
 
Scott Selikoff
author
Posts: 4323
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many languages support the + operator in this manner so it’s not uncommon at all.
 
Frank Mi
Rancher
Posts: 129
15
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Scott,

In my understanding, operator overloading means an operator can be used on more than one function, that is why I thought "+" is overloaded here. Something is not legit in programming doesn't necessarily mean the language throws the technique away.

But anyway, if you say the meaning of "user defined" is implicitly included, I trust you.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding on to what Scott said.... to make things abundantly clear.....

Conceptually, an "overloaded operator" exists in a language where I can change the meaning of the operator for a particular object (only objects not primitives AFAIK).
If you look at examples in c++ you can instantly tell how this works: https://www.programiz.com/cpp-programming/operator-overloading

This link gives a really good definition of why what Java is doing is not "operator overloading": https://www.tutorialspoint.com/why-is-operator-overloading-not-supported-by-java


If we want to talk about technicalities and interesting nuances.....

The spec refers to what we call the token that is the plus sign in several contexts, and defines concretely what they do in each of those contexts:
Unary plus operator: https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.15.3
Additive operators (both primitive addition and String concatenation operators): https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.18

The token itself is a different operator based on the context in which it occurs (and has a different name, ex unary plus operator vs concatenation operator). The compiler knows what the context is, and generates different instructions based on that information.

We can see evidence of what's going on when we examine the instructions generated by the compiler.

Example:


When we compile and run javap -c on the class file we can identify that these are, in fact, two completely different instructions with similar human readable syntax:

9: invokedynamic #3,  0              // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
...
24: iadd


Cool, huh?
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic