CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Why can relational operators be used for char variables but not String objects?

  1. #1
    Join Date
    Jan 2009
    Posts
    11

    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?

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Why can relational operators be used for char variables but not String objects?

    Quote Originally Posted by jd53887 View Post
    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.

  3. #3
    Join Date
    Mar 2010
    Posts
    74

    Re: Why can relational operators be used for char variables but not String objects?

    Quote Originally Posted by jd53887 View Post
    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?

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)