X-Received: by 10.66.246.197 with SMTP id xy5mr4746684pac.47.1472620823280; Tue, 30 Aug 2016 22:20:23 -0700 (PDT) X-Received: by 10.157.41.140 with SMTP id n12mr620610otb.18.1472620823240; Tue, 30 Aug 2016 22:20:23 -0700 (PDT) Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.glorb.com!n75no109356ith.0!news-out.google.com!b4ni5301iti.0!nntp.google.com!n75no109347ith.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Tue, 30 Aug 2016 22:20:16 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2602:304:b20e:d790:3c61:ba10:9231:1216; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T NNTP-Posting-Host: 2602:304:b20e:d790:3c61:ba10:9231:1216 References: <33463070-424f-4fb0-956b-2849f1a400eb@googlegroups.com> <1c9ad1a1-ee44-4a9f-a307-1dab14c4a921@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Why not String compare? From: Lew Injection-Date: Wed, 31 Aug 2016 05:20:23 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.programmer:35508 Eric Douglas wrote: > Robert Klemme wrote: > > Eric Douglas wrote: > > > Robert Klemme wrote: > > > http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#co= mpareTo-T- > > >> > > > > > > What's your point? Did you read the question? > >=20 > > Did you read the documentation? >=20 > Do you always answer a question with a question? > Yes, if you read my message, I am well aware the default for the Comparab= le class is to=20 > throw NPE if you try to compareTo(null). =20 "Default"? It's not the "default". If you actually had read the documentati= on 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.=20 Because it's required. The docs say so. > This doesn't make sense.=20 Sounds like your problem, not the documentation's. It makes sense to most J= ava programmers: The docs say to throw an NPE if the argument is null. So t= he implementation did. That seems awfully easy to understand. > myString.compareTo(null) should return 1, just as myString.equals(null) r= eturns false. Why=20 No, it shouldn't. That would violate the requirement, and code isn't suppos= ed 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?=20 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 sillines= s, 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 glorio= us Platonic ideal "it should be". This is just your personal opinion for wh= ich you have not offered a jot of any objective rationale, but againsty whi= ch there is ample objective rationale. Like that fact that NULL (represente= d 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 arrogan= t judgment pronounce that it is dumb". It only seems dumb to you, because y= ou aren't thinking about it as offered to you. Perhaps you should abandon y= our prejudices and embrace things as they are. Happy to help. --=20 Lew