src/share/classes/java/lang/Long.java

Print this page
rev 4568 : 7088952: Add "BYTES" constant to primitive wrapper classes
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
Reviewed-by: smarks


 769         return toString(value);
 770     }
 771 
 772     /**
 773      * Returns a hash code for this {@code Long}. The result is
 774      * the exclusive OR of the two halves of the primitive
 775      * {@code long} value held by this {@code Long}
 776      * object. That is, the hashcode is the value of the expression:
 777      *
 778      * <blockquote>
 779      *  {@code (int)(this.longValue()^(this.longValue()>>>32))}
 780      * </blockquote>
 781      *
 782      * @return  a hash code value for this object.
 783      */
 784     public int hashCode() {
 785         return (int)(value ^ (value >>> 32));
 786     }
 787 
 788     /**












 789      * Compares this object to the specified object.  The result is
 790      * {@code true} if and only if the argument is not
 791      * {@code null} and is a {@code Long} object that
 792      * contains the same {@code long} value as this object.
 793      *
 794      * @param   obj   the object to compare with.
 795      * @return  {@code true} if the objects are the same;
 796      *          {@code false} otherwise.
 797      */
 798     public boolean equals(Object obj) {
 799         if (obj instanceof Long) {
 800             return value == ((Long)obj).longValue();
 801         }
 802         return false;
 803     }
 804 
 805     /**
 806      * Determines the {@code long} value of the system property
 807      * with the specified name.
 808      *


 963      * @param  y the second {@code long} to compare
 964      * @return the value {@code 0} if {@code x == y};
 965      *         a value less than {@code 0} if {@code x < y}; and
 966      *         a value greater than {@code 0} if {@code x > y}
 967      * @since 1.7
 968      */
 969     public static int compare(long x, long y) {
 970         return (x < y) ? -1 : ((x == y) ? 0 : 1);
 971     }
 972 
 973 
 974     // Bit Twiddling
 975 
 976     /**
 977      * The number of bits used to represent a {@code long} value in two's
 978      * complement binary form.
 979      *
 980      * @since 1.5
 981      */
 982     public static final int SIZE = 64;








 983 
 984     /**
 985      * Returns a {@code long} value with at most a single one-bit, in the
 986      * position of the highest-order ("leftmost") one-bit in the specified
 987      * {@code long} value.  Returns zero if the specified value has no
 988      * one-bits in its two's complement binary representation, that is, if it
 989      * is equal to zero.
 990      *
 991      * @return a {@code long} value with a single one-bit, in the position
 992      *     of the highest-order one-bit in the specified value, or zero if
 993      *     the specified value is itself equal to zero.
 994      * @since 1.5
 995      */
 996     public static long highestOneBit(long i) {
 997         // HD, Figure 3-1
 998         i |= (i >>  1);
 999         i |= (i >>  2);
1000         i |= (i >>  4);
1001         i |= (i >>  8);
1002         i |= (i >> 16);




 769         return toString(value);
 770     }
 771 
 772     /**
 773      * Returns a hash code for this {@code Long}. The result is
 774      * the exclusive OR of the two halves of the primitive
 775      * {@code long} value held by this {@code Long}
 776      * object. That is, the hashcode is the value of the expression:
 777      *
 778      * <blockquote>
 779      *  {@code (int)(this.longValue()^(this.longValue()>>>32))}
 780      * </blockquote>
 781      *
 782      * @return  a hash code value for this object.
 783      */
 784     public int hashCode() {
 785         return (int)(value ^ (value >>> 32));
 786     }
 787 
 788     /**
 789      * Returns a hash code for a {@code long} value; compatible with
 790      * {@code Long.hashCode()}.
 791      *  
 792      * @since 1.8
 793      *
 794      * @return a hash code value for a {@code long} value.
 795      */
 796     public static int hashCode(long value) {
 797         return (int)(value ^ (value >>> 32));
 798     }
 799 
 800     /**
 801      * Compares this object to the specified object.  The result is
 802      * {@code true} if and only if the argument is not
 803      * {@code null} and is a {@code Long} object that
 804      * contains the same {@code long} value as this object.
 805      *
 806      * @param   obj   the object to compare with.
 807      * @return  {@code true} if the objects are the same;
 808      *          {@code false} otherwise.
 809      */
 810     public boolean equals(Object obj) {
 811         if (obj instanceof Long) {
 812             return value == ((Long)obj).longValue();
 813         }
 814         return false;
 815     }
 816 
 817     /**
 818      * Determines the {@code long} value of the system property
 819      * with the specified name.
 820      *


 975      * @param  y the second {@code long} to compare
 976      * @return the value {@code 0} if {@code x == y};
 977      *         a value less than {@code 0} if {@code x < y}; and
 978      *         a value greater than {@code 0} if {@code x > y}
 979      * @since 1.7
 980      */
 981     public static int compare(long x, long y) {
 982         return (x < y) ? -1 : ((x == y) ? 0 : 1);
 983     }
 984 
 985 
 986     // Bit Twiddling
 987 
 988     /**
 989      * The number of bits used to represent a {@code long} value in two's
 990      * complement binary form.
 991      *
 992      * @since 1.5
 993      */
 994     public static final int SIZE = 64;
 995 
 996     /**
 997      * The number of bytes used to represent a {@code long} value in two's
 998      * complement binary form.
 999      *
1000      * @since 1.8
1001      */
1002     public static final int BYTES = SIZE / Byte.SIZE;
1003 
1004     /**
1005      * Returns a {@code long} value with at most a single one-bit, in the
1006      * position of the highest-order ("leftmost") one-bit in the specified
1007      * {@code long} value.  Returns zero if the specified value has no
1008      * one-bits in its two's complement binary representation, that is, if it
1009      * is equal to zero.
1010      *
1011      * @return a {@code long} value with a single one-bit, in the position
1012      *     of the highest-order one-bit in the specified value, or zero if
1013      *     the specified value is itself equal to zero.
1014      * @since 1.5
1015      */
1016     public static long highestOneBit(long i) {
1017         // HD, Figure 3-1
1018         i |= (i >>  1);
1019         i |= (i >>  2);
1020         i |= (i >>  4);
1021         i |= (i >>  8);
1022         i |= (i >> 16);