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


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

Re: Comparison structure?

From j4n bur53 <janburse@fastmail.fm>
Newsgroups comp.lang.java.programmer
Subject Re: Comparison structure?
Date 2016-10-05 17:12 +0200
Organization albasani.net
Message-ID <nt358q$lld$1@news.albasani.net> (permalink)
References <59c17fe5-61a6-46dc-918f-ee5aa348e3fd@googlegroups.com> <nt0pcj$r9u$1@dont-email.me>

Show all headers | View raw


Martin Gregorie schrieb:
> Three ways is always a bit of a toss-up because it looks messy any way
> you implement it.

since JDK 1.7 there is the class Integer:

     public static int compare(int x, int y);

which avoids the boxing overhead.
the contract says <0, 0, >0,
but the implementation does -1,0,+1

(historically compare to could be implemented
by x-y, if |x| and |y| are less than 2^30,
because then the difference can be correctly
represented with the desired sign,

this works fine for byte, short and
char, if int widening is used, we therefore
find in JDK 1.7 in the corresponding wrapper
classes:

     public static int compare(byte x, byte y) {
         return x - y;
     }

     public static int compare(short x, short y) {
         return x - y;
     }

     public static int compare(char x, char y) {
         return x - y;
     }

but the exception is int, since for int
arguments the condition |x| and |y| are less
than 2^30, since the int range is one binary
magnitude bigger, similar problem with long:

     public static int compare(int x, int y) {
         return (x < y) ? -1 : ((x == y) ? 0 : 1);
     }

     public static int compare(long x, long y) {
         return (x < y) ? -1 : ((x == y) ? 0 : 1);
     }




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


Thread

Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 07:25 -0700
  Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-04 10:46 -0400
  Re: Comparison structure? Roedy Green <inquiry@mindprod.com> - 2016-10-04 09:53 -0700
    Re: Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 10:29 -0700
      Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-04 15:35 -0400
        Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 20:16 -0400
          Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 22:29 -0400
            Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-05 08:08 -0400
              Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-05 20:27 -0400
  Re: Comparison structure? Martin Gregorie <martin@address-in-sig.invalid> - 2016-10-04 17:37 +0000
    Re: Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 12:02 -0700
    Re: Comparison structure? j4n bur53 <janburse@fastmail.fm> - 2016-10-05 17:12 +0200
      Re: Comparison structure? j4n bur53 <janburse@fastmail.fm> - 2016-10-05 17:14 +0200
  Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 20:03 -0400
  Re: Comparison structure? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-10-05 22:41 +0200

csiph-web