src/share/classes/java/lang/Byte.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 427         return compare(this.value, anotherByte.value);
 428     }
 429 
 430     /**
 431      * Compares two {@code byte} values numerically.
 432      * The value returned is identical to what would be returned by:
 433      * <pre>
 434      *    Byte.valueOf(x).compareTo(Byte.valueOf(y))
 435      * </pre>
 436      *
 437      * @param  x the first {@code byte} to compare
 438      * @param  y the second {@code byte} to compare
 439      * @return the value {@code 0} if {@code x == y};
 440      *         a value less than {@code 0} if {@code x < y}; and
 441      *         a value greater than {@code 0} if {@code x > y}
 442      * @since 1.7
 443      */
 444     public static int compare(byte x, byte y) {
 445         return x - y;
 446     }









































 447 
 448     /**
 449      * The number of bits used to represent a {@code byte} value in two's
 450      * complement binary form.
 451      *
 452      * @since 1.5
 453      */
 454     public static final int SIZE = 8;
 455 
 456     /** use serialVersionUID from JDK 1.1. for interoperability */
 457     private static final long serialVersionUID = -7183698231559129828L;
 458 }
   1 /*
   2  * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 427         return compare(this.value, anotherByte.value);
 428     }
 429 
 430     /**
 431      * Compares two {@code byte} values numerically.
 432      * The value returned is identical to what would be returned by:
 433      * <pre>
 434      *    Byte.valueOf(x).compareTo(Byte.valueOf(y))
 435      * </pre>
 436      *
 437      * @param  x the first {@code byte} to compare
 438      * @param  y the second {@code byte} to compare
 439      * @return the value {@code 0} if {@code x == y};
 440      *         a value less than {@code 0} if {@code x < y}; and
 441      *         a value greater than {@code 0} if {@code x > y}
 442      * @since 1.7
 443      */
 444     public static int compare(byte x, byte y) {
 445         return x - y;
 446     }
 447 
 448     /**
 449      * Converts the argument to an {@code int} by an unsigned
 450      * conversion.  In an unsigned conversion to an {@code int}, the
 451      * high-order 24 bits of the {@code int} are zero and the
 452      * low-order 8 bits are equal to the bits of the {@code byte} argument.
 453      *
 454      * Consequently, zero and positive {@code byte} values are mapped
 455      * to a numerically equal {@code int} value and negative {@code
 456      * byte} values are mapped to an {@code int} value equal to the
 457      * input plus 2<sup>8</sup>.
 458      *
 459      * @param  x the value to convert to an unsigned {@code int}
 460      * @return the argument converted to {@code int} by an unsigned
 461      *         conversion
 462      * @since 1.8
 463      */
 464     public static int toUnsignedInt(byte x) {
 465         return ((int) x) & 0xff;
 466     }
 467 
 468     /**
 469      * Converts the argument to a {@code long} by an unsigned
 470      * conversion.  In an unsigned conversion to a {@code long}, the
 471      * high-order 56 bits of the {@code long} are zero and the
 472      * low-order 8 bits are equal to the bits of the {@code byte} argument.
 473      *
 474      * Consequently, zero and positive {@code byte} values are mapped
 475      * to a numerically equal {@code long} value and negative {@code
 476      * byte} values are mapped to a {@code long} value equal to the
 477      * input plus 2<sup>8</sup>.
 478      *
 479      * @param  x the value to convert to an unsigned {@code long}
 480      * @return the argument converted to {@code long} by an unsigned
 481      *         conversion
 482      * @since 1.8
 483      */
 484     public static long toUnsignedLong(byte x) {
 485         return ((long) x) & 0xffL;
 486     }
 487 
 488 
 489     /**
 490      * The number of bits used to represent a {@code byte} value in two's
 491      * complement binary form.
 492      *
 493      * @since 1.5
 494      */
 495     public static final int SIZE = 8;
 496 
 497     /** use serialVersionUID from JDK 1.1. for interoperability */
 498     private static final long serialVersionUID = -7183698231559129828L;
 499 }