Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35508

Re: Why not String compare?

Newsgroups comp.lang.java.programmer
Date 2016-08-30 22:20 -0700
References <33463070-424f-4fb0-956b-2849f1a400eb@googlegroups.com> <e2ba8jF67mfU1@mid.individual.net> <1c9ad1a1-ee44-4a9f-a307-1dab14c4a921@googlegroups.com> <e2bo9iF9m4nU1@mid.individual.net> <d9d392a6-7361-4743-aaef-22ec3d126722@googlegroups.com>
Message-ID <c00873ef-4b47-4e26-a0d9-e61bbcdbacca@googlegroups.com> (permalink)
Subject Re: Why not String compare?
From Lew <lewbloch@gmail.com>

Show all headers | View raw


Eric Douglas wrote:
> Robert Klemme wrote:
> > Eric Douglas wrote:
> > > Robert Klemme wrote:
> > > http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#compareTo-T-
> > >>
> > >
> > > What's your point?  Did you read the question?
> > 
> > Did you read the documentation?


> 
> Do you always answer a question with a question?
> Yes, if you read my message, I am well aware the default for the Comparable class is to 
> throw NPE if you try to compareTo(null).  

"Default"? It's not the "default". If you actually had read the documentation you would know that.

Here's what the documentation says:
"Throws:
NullPointerException - if the specified object is null"

Not as a "default". As the requirement.

The requirement.

It's required.

That's why the implementor did it, to satisfy the requirement, as required.

Not as a "default".

> I'm asking why. 

Because it's required.

The docs say so.

> This doesn't make sense. 

Sounds like your problem, not the documentation's. It makes sense to most Java programmers: The docs say to throw an NPE if the argument is null. So the implementation did.

That seems awfully easy to understand.

> myString.compareTo(null) should return 1, just as myString.equals(null) returns false.  Why 

No, it shouldn't. That would violate the requirement, and code isn't supposed to violate requirements. Why would you think that it should?

> shouldn't it make sense for everything to reutrn 1 (greater than) if you compare to a null? 

Nope. Because NULL is neither greater than, less than, equal to, or unequal to anything else in maths. That it sometimes does in computers is silliness, and anyway, "it makes sense" or not is an entirely subjective assessment. You should phrase it more responsibly as "I say that", not as some glorious Platonic ideal "it should be". This is just your personal opinion for which you have not offered a jot of any objective rationale, but againsty which there is ample objective rationale. Like that fact that NULL (represented by `null`) is an out-of-band value and therefore not sensibly comparable to in-bad values

> Is there another value to pass into the String.compareTo() to return the lowest possible String?  This seems dumb that everyone who wants to compare Strings has to null check first.

Again with "seems dumb" as a cover for "I in my infinite wisdom and arrogant judgment pronounce that it is dumb". It only seems dumb to you, because you aren't thinking about it as offered to you. Perhaps you should abandon your prejudices and embrace things as they are.

Happy to help.

-- 
Lew

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 09:00 -0700
  Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-26 18:49 +0200
    Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 10:09 -0700
      Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-26 22:48 +0200
        Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 13:55 -0700
          Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-26 17:29 -0400
          Re: Why not String compare? Lew <lewbloch@gmail.com> - 2016-08-30 22:20 -0700
          Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-31 08:17 +0200
            Re: Why not String compare? Andreas Leitgeb <avl@logic.at> - 2016-08-31 09:45 +0000
              Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-08-31 19:24 +0200
                Re: Why not String compare? Andreas Leitgeb <avl@logic.at> - 2016-09-01 10:17 +0000
                Re: Why not String compare? Robert Klemme <shortcutter@googlemail.com> - 2016-09-02 18:29 +0200
  Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-26 13:24 -0400
    Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 10:48 -0700
      Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-26 14:50 -0400
        Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 12:19 -0700
      Re: Why not String compare? Lothar Kimmeringer <news200709@kimmeringer.de> - 2016-09-01 20:42 +0200
        Re: Why not String compare? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-01 17:57 -0400
    Re: Why not String compare? Eric Douglas <e.d.programmer@gmail.com> - 2016-08-26 11:03 -0700
      Re: Why not String compare? Lothar Kimmeringer <news200709@kimmeringer.de> - 2016-09-02 21:54 +0200

csiph-web