src/share/classes/java/lang/Integer.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


 739      *
 740      * @return  a string representation of the value of this object in
 741      *          base 10.
 742      */
 743     public String toString() {
 744         return toString(value);
 745     }
 746 
 747     /**
 748      * Returns a hash code for this {@code Integer}.
 749      *
 750      * @return  a hash code value for this object, equal to the
 751      *          primitive {@code int} value represented by this
 752      *          {@code Integer} object.
 753      */
 754     public int hashCode() {
 755         return value;
 756     }
 757 
 758     /**












 759      * Compares this object to the specified object.  The result is
 760      * {@code true} if and only if the argument is not
 761      * {@code null} and is an {@code Integer} object that
 762      * contains the same {@code int} value as this object.
 763      *
 764      * @param   obj   the object to compare with.
 765      * @return  {@code true} if the objects are the same;
 766      *          {@code false} otherwise.
 767      */
 768     public boolean equals(Object obj) {
 769         if (obj instanceof Integer) {
 770             return value == ((Integer)obj).intValue();
 771         }
 772         return false;
 773     }
 774 
 775     /**
 776      * Determines the integer value of the system property with the
 777      * specified name.
 778      *


1016      * @param  y the second {@code int} to compare
1017      * @return the value {@code 0} if {@code x == y};
1018      *         a value less than {@code 0} if {@code x < y}; and
1019      *         a value greater than {@code 0} if {@code x > y}
1020      * @since 1.7
1021      */
1022     public static int compare(int x, int y) {
1023         return (x < y) ? -1 : ((x == y) ? 0 : 1);
1024     }
1025 
1026 
1027     // Bit twiddling
1028 
1029     /**
1030      * The number of bits used to represent an {@code int} value in two's
1031      * complement binary form.
1032      *
1033      * @since 1.5
1034      */
1035     public static final int SIZE = 32;








1036 
1037     /**
1038      * Returns an {@code int} value with at most a single one-bit, in the
1039      * position of the highest-order ("leftmost") one-bit in the specified
1040      * {@code int} value.  Returns zero if the specified value has no
1041      * one-bits in its two's complement binary representation, that is, if it
1042      * is equal to zero.
1043      *
1044      * @return an {@code int} value with a single one-bit, in the position
1045      *     of the highest-order one-bit in the specified value, or zero if
1046      *     the specified value is itself equal to zero.
1047      * @since 1.5
1048      */
1049     public static int highestOneBit(int i) {
1050         // HD, Figure 3-1
1051         i |= (i >>  1);
1052         i |= (i >>  2);
1053         i |= (i >>  4);
1054         i |= (i >>  8);
1055         i |= (i >> 16);




 739      *
 740      * @return  a string representation of the value of this object in
 741      *          base&nbsp;10.
 742      */
 743     public String toString() {
 744         return toString(value);
 745     }
 746 
 747     /**
 748      * Returns a hash code for this {@code Integer}.
 749      *
 750      * @return  a hash code value for this object, equal to the
 751      *          primitive {@code int} value represented by this
 752      *          {@code Integer} object.
 753      */
 754     public int hashCode() {
 755         return value;
 756     }
 757 
 758     /**
 759      * Returns a hash code for a {@code int} value; compatible with
 760      * {@code Integer.hashCode()}.
 761      *  
 762      * @since 1.8
 763      *
 764      * @return a hash code value for a {@code int} value.
 765      */
 766     public static int hashCode(int value) {
 767         return value;
 768     }
 769 
 770     /**
 771      * Compares this object to the specified object.  The result is
 772      * {@code true} if and only if the argument is not
 773      * {@code null} and is an {@code Integer} object that
 774      * contains the same {@code int} value as this object.
 775      *
 776      * @param   obj   the object to compare with.
 777      * @return  {@code true} if the objects are the same;
 778      *          {@code false} otherwise.
 779      */
 780     public boolean equals(Object obj) {
 781         if (obj instanceof Integer) {
 782             return value == ((Integer)obj).intValue();
 783         }
 784         return false;
 785     }
 786 
 787     /**
 788      * Determines the integer value of the system property with the
 789      * specified name.
 790      *


1028      * @param  y the second {@code int} to compare
1029      * @return the value {@code 0} if {@code x == y};
1030      *         a value less than {@code 0} if {@code x < y}; and
1031      *         a value greater than {@code 0} if {@code x > y}
1032      * @since 1.7
1033      */
1034     public static int compare(int x, int y) {
1035         return (x < y) ? -1 : ((x == y) ? 0 : 1);
1036     }
1037 
1038 
1039     // Bit twiddling
1040 
1041     /**
1042      * The number of bits used to represent an {@code int} value in two's
1043      * complement binary form.
1044      *
1045      * @since 1.5
1046      */
1047     public static final int SIZE = 32;
1048 
1049     /**
1050      * The number of bytes used to represent a {@code int} value in two's
1051      * complement binary form.
1052      *
1053      * @since 1.8
1054      */
1055     public static final int BYTES = SIZE / Byte.SIZE;
1056 
1057     /**
1058      * Returns an {@code int} value with at most a single one-bit, in the
1059      * position of the highest-order ("leftmost") one-bit in the specified
1060      * {@code int} value.  Returns zero if the specified value has no
1061      * one-bits in its two's complement binary representation, that is, if it
1062      * is equal to zero.
1063      *
1064      * @return an {@code int} value with a single one-bit, in the position
1065      *     of the highest-order one-bit in the specified value, or zero if
1066      *     the specified value is itself equal to zero.
1067      * @since 1.5
1068      */
1069     public static int highestOneBit(int i) {
1070         // HD, Figure 3-1
1071         i |= (i >>  1);
1072         i |= (i >>  2);
1073         i |= (i >>  4);
1074         i |= (i >>  8);
1075         i |= (i >> 16);