|
-
March 26th, 2010, 06:07 PM
#1
Why can relational operators be used for char variables but not String objects?
Why can relational operators be used for char variables but not String objects?
-
March 27th, 2010, 01:05 AM
#2
Re: Why can relational operators be used for char variables but not String objects?
 Originally Posted by jd53887
Why can relational operators be used for char variables but not String objects?
The Java designers took a number of steps to make Java simple. One of the most far reaching was the strict divide between primitives and objects, where primitives have value semantics only and objects have reference semantics only. To somewhat soften this divide several measures have been taken. One was to give the String class special language support to make it behave almost like a primitive. Another was autoboxing which introduced type coersion between the primitives and their wrapper classes. Still, to fully bridge the gap between primitives and objects would introduce many complexities the Java designers tried to avoid in the first place, so I guess we'll have to live with it. Or you can use C++ or C# instead. Especially C++ goes to great lengths to allow objects to behave like primitives if you so choose.
Many would say the simplicity of Java by far outweights the negative effects of the rift between primitives and objects. And how hard is it really to use the equals and compareTo methods with objects rather than relational operators?
Last edited by nuzzle; March 27th, 2010 at 02:11 AM.
-
March 27th, 2010, 09:24 AM
#3
Re: Why can relational operators be used for char variables but not String objects?
 Originally Posted by jd53887
Why can relational operators be used for char variables but not String objects?
You can always create your own "relational" rule for String objects and realize it on a basis of relational operators for char variables.
Why put more complexity to Java?
-
March 27th, 2010, 11:10 AM
#4
Re: Why can relational operators be used for char variables but not String objects?
Primitives like char are simple numeric types, so comparisons are numeric and the results of relational operators are clear and unambiguous.
String is an exception among the objects in having some overloaded operators, but it has multiple potential modes of comparison, including straight lexicographic, case-insensitive, numeric comparison, and others. Relational operators can only handle one of these modes, so it was not thought appropriate to provide them.
It is better to have an approximate answer to the right question than an exact answer to the wrong one...
J. Tukey
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|