Timeline for Why didn't == operator string value comparison make it to Java?
Current License: CC BY-SA 3.0
16 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 14, 2016 at 22:43 | comment | added | Andres F. |
@Blrfl It was a rhetorical question; I'm not actually asking about + vs == (I know how they work and why, I'm not a Java novice), but instead pointing out it's false this is about operator overloading. Read the currently accepted answer to understand why.
|
|
| Mar 14, 2016 at 20:04 | comment | added | Blrfl |
@AndresF.: You asked why + works on Strings and I explained why. I would suggest that instead of carrying on the same discussion in the comments of three answers, you formulate your concerns into a question.
|
|
| Mar 14, 2016 at 19:10 | comment | added | Andres F. | @Blrfl Sure. I'm saying the "Java has no operator overloading" part of your answer is a red herring. | |
| Mar 14, 2016 at 19:01 | comment | added | Blrfl | @AndresF.: See my earlier comment about why a special case for equality vs. reference comparison. | |
| Mar 14, 2016 at 18:26 | comment | added | Andres F. |
@Blrfl Indeed, that was my point. Since + is a special case, so could have been ==. Having it compare String references instead of comparing Strings has nothing to do with operator overloading.
|
|
| Mar 14, 2016 at 18:10 | comment | added | Blrfl |
@AndresF.: The concatenation (+) operator for strings is a special case that's built into the language. To repeat the first sentence in the answer, Java does not provide facilities for general-purpose operator overloading.
|
|
| Mar 14, 2016 at 15:10 | comment | added | Andres F. |
But "hello " + "world!" works, so this can't be the answer. Otherwise, why does + work on Strings?
|
|
| Apr 13, 2013 at 12:11 | comment | added | Giorgio |
I think you can see a reference as a primitive type. In fact, primitive types are the only types that can be used in assignment, comparison (through ==), and that can be passed (by value) as method arguments. This keeps the language simpler.
|
|
| Apr 2, 2013 at 11:41 | comment | added | Gilad Naaman | @Blrfl: I know, I wish it had. | |
| Apr 2, 2013 at 11:40 | comment | added | Blrfl | @GiladNaaman: C# has that. Java doesn't. | |
| Apr 2, 2013 at 11:29 | comment | added | Gilad Naaman |
@Blrfl: Not really. There will always be a defined way to compare reference (ClassName.ReferenceEquals(a,b)), and a default == operator and Equals method both pointing to ReferenceEquals.
|
|
| Apr 2, 2013 at 11:16 | comment | added | Blrfl |
@GiladNaaman: That would be a zero-sum game because it causes the opposite problem to what Java has now: equality would be on an operator and you'd have to invoke a method to compare references. Further, you'd have to impose the requirement that all classes implement something that can be bound to ==. That's effectively adding operator overloading, which would have tremendous implications on the way Java is implemented.
|
|
| Apr 2, 2013 at 11:08 | comment | added | Gilad Naaman | Although that can be solved by a static method that compares the references of two objects (or an operator). Like in C#, for example. | |
| Apr 2, 2013 at 11:05 | comment | added | Blrfl | @Giorgio: Exactly. See my comment on Gilad Naaman's answer. | |
| Apr 2, 2013 at 11:03 | comment | added | Giorgio |
"Java doesn't support operator overloading, which means == only applies to primitive types or references. Anything else requires invocation of a method.": One could add that if == meant string equality, we would need another notation for reference equality.
|
|
| Apr 2, 2013 at 10:59 | history | answered | Blrfl | CC BY-SA 3.0 |