Integer类【JDK源码分析】
前言
2022/10/24
路漫漫其修远兮,吾将上下而求索
本文是根据jdk学习所做笔记
仅供学习交流使用,转载注明出处
推荐
JDK API 1.6 中文版
说明
以下内容是结合很多资料进行编写的
源码为jdk1.8的
斜体样式 为自己的思考
下划线为自己所画的重点
Integer类
基本信息
java.lang
类 Integer
java.lang.Object
继承者 java.lang.Number
继承者 java.lang.Integer
所有已实现的接口:
Serializable, Comparable
public final class Integerextends Numberimplements ComparableInteger 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个 int 类型的字段。
此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。
实现注意事项:“bit twiddling”方法(如 highestOneBit 和 numberOfTrailingZeros)的实现基于 Henry S. Warren, Jr.撰写的 Hacker’s Delight(Addison Wesley, 2002)中的一些有关材料。
从以下版本开始:
JDK1.0
另请参见:
序列化表格
字段摘要
字段 | 说明 |
---|---|
static int MAX_VALUE | 值为 231-1 的常量,它表示 int 类型能够表示的最大值。 |
static int MIN_VALUE | 值为 -231 的常量,它表示 int 类型能够表示的最小值。 |
static int SIZE | 用来以二进制补码形式表示 int 值的比特位数。 |
static Class TYPE | 表示基本类型 int 的 Class 实例。 |
/**
* A constant holding the minimum value an {@code int} can
* have, -2<sup>31</sup>.
*/
@Native public static final int MIN_VALUE = 0x80000000;
/**
* A constant holding the maximum value an {@code int} can
* have, 2<sup>31</sup>-1.
*/
@Native public static final int MAX_VALUE = 0x7fffffff;
/**
* The {@code Class} instance representing the primitive type
* {@code int}.
*
* @since JDK1.1
*/
@SuppressWarnings("unchecked")
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
/**
* All possible chars for representing a number as a String
*/
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
内部类
享元模式
缓存支持JLS要求的* -128和127(含)之间值的自动装箱的对象标识语义。*
缓存在第一次使用时初始化。缓存的大小可以由{ @ code-XX:AutoBoxCacheMax = < size > }选项控制。
在虚拟机初始化期间,Java . lang . integer . integer cache . high属性可能会被设置并保存在sun.misc.VM class
high可以配置,但是要包含[-128,127]
IntegerCache
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
部分方法
XxxValue
Integer转为Xxx基本数据类型
public Xxx XxxValue() {
return (Xxx)value;
}
例如
/**
* Returns the value of this {@code Integer} as an
* {@code int}.
*/
public int intValue() {
return value;
}
equals
public boolean equals(Object obj)比较此对象与指定对象。当且仅当参数不为 null,并且是一个与该对象包含相同 int 值的 Integer 对象时,结果为 true。
覆盖:
类 Object 中的 equals
参数:
obj - 要比较的对象。
返回:
如果对象相同,则返回 true,否则返回 false。
另请参见:
Object.hashCode(), Hashtable
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
hashCode
public int hashCode()返回此 Integer 的哈希码。
覆盖:
类 Object 中的 hashCode
返回:
该对象的哈希码值,它的值即为该 Integer 对象表示的基本 int 类型的数值。
另请参见:
Object.equals(java.lang.Object), Hashtable
@Override
public int hashCode() {
return Integer.hashCode(value);
}
/**
* Returns a hash code for a {@code int} value; compatible with
* {@code Integer.hashCode()}.
*
* @param value the value to hash
* @since 1.8
*
* @return a hash code value for a {@code int} value.
*/
public static int hashCode(int value) {
return value;
}
highestOneBit
Integer 的 Integer.highestOneBit方法源码解读
传入一个int参数i,返回其二进制最高位1的权值。
- 若i为正整数,例如i=9,其二进制为1001,最高位1的权值为8,则返回值为8
- 若i为负整数,最高位1为符号位,返回值为-2147483648
- 若i为零,返回值为0
public static int highestOneBit(int i)
返回具有至多单个 1 位的 int 值,在指定的 int 值中最高位(最左边)的 1 位的位置。如果指定的值在其二进制补码表示形式中不具有 1 位,即它等于零,则返回零。
返回:
返回具有单个 1 位的 int 值, 在指定值中最高位的 1 位的位置,否则,如果指定值本身等于零,则返回零。
从以下版本开始:
1.5
/**
* Returns an {@code int} value with at most a single one-bit, in the
* position of the highest-order ("leftmost") one-bit in the specified
* {@code int} value. Returns zero if the specified value has no
* one-bits in its two's complement binary representation, that is, if it
* is equal to zero.
*
* @param i the value whose highest one bit is to be computed
* @return an {@code int} value with a single one-bit, in the position
* of the highest-order one-bit in the specified value, or zero if
* the specified value is itself equal to zero.
* @since 1.5
*/
public static int highestOneBit(int i) {
// HD, Figure 3-1
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);//保证把最高位的1及其后面所有位变成1
return i - (i >>> 1);//减去,把(最高位的1变成0)了后面所有位变成1 这样返回的就是最高位的权值了
}
lowestOneBit
传入一个int参数i,返回其二进制最低位1的权值。
public static int lowestOneBit(int i)返回具有至多单个 1 位的 int 值,在指定的 int 值中最低位(最右边)的 1 位的位置。如果指定的值在其二进制补码表示形式中不具有 1 位,即它等于零,则返回零。
返回:
返回具有单个 1 位的 int 值, 在指定值中最低位的 1 位的位置,否则,如果指定值本身等于零,则返回零。
从以下版本开始:
1.5
/**
* Returns an {@code int} value with at most a single one-bit, in the
* position of the lowest-order ("rightmost") one-bit in the specified
* {@code int} value. Returns zero if the specified value has no
* one-bits in its two's complement binary representation, that is, if it
* is equal to zero.
*
* @param i the value whose lowest one bit is to be computed
* @return an {@code int} value with a single one-bit, in the position
* of the lowest-order one-bit in the specified value, or zero if
* the specified value is itself equal to zero.
* @since 1.5
*/
public static int lowestOneBit(int i) {
// HD, Section 2-1
return i & -i;
}
ValueOf
基本数据类型、String转为Integer
二进制相关
从类 java.lang.Object 继承的方法
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
测试
package testlang;
/**
* @author CSDN@日星月云
* @date 2022/10/24 22:32
*/
public class testInteger {
public static void main(String[] args) {
Integer i=new Integer(15);
System.out.println(i.toString());//15
System.out.println(Integer.lowestOneBit(i));//1 传入一个int参数i,返回其二进制最低位1的权值
System.out.println(Integer.highestOneBit(i));//8 传入一个int参数i,返回其二进制最高位1的权值
}
}
另外
其余位操作
numberOfLeadingZeros
public static int numberOfLeadingZeros(int i)
在指定 int 值的二进制补码表示形式中最高位(最左边)的 1 位之前,返回零位的数量。如果指定值在其二进制补码表示形式中不存在 1 位,换句话说,如果它等于零,则返回 32。
注意,此方法与基数为 2 的对数密切相关。对于所有的正 int 值 x:
floor(log2(x)) = 31 - numberOfLeadingZeros(x)
ceil(log2(x)) = 32 - numberOfLeadingZeros(x - 1)
返回:
返回在指定 int 值的二进制补码表示形式中最高位(最左边)的 1 位之前的零位的数量;否则,如果该值等于零,则返回 32。
从以下版本开始:
1.5
/**
* Returns the number of zero bits preceding the highest-order
* ("leftmost") one-bit in the two's complement binary representation
* of the specified {@code int} value. Returns 32 if the
* specified value has no one-bits in its two's complement representation,
* in other words if it is equal to zero.
*
* <p>Note that this method is closely related to the logarithm base 2.
* For all positive {@code int} values x:
* <ul>
* <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)}
* <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}
* </ul>
*
* @param i the value whose number of leading zeros is to be computed
* @return the number of zero bits preceding the highest-order
* ("leftmost") one-bit in the two's complement binary representation
* of the specified {@code int} value, or 32 if the value
* is equal to zero.
* @since 1.5
*/
public static int numberOfLeadingZeros(int i) {
// HD, Figure 5-6
if (i == 0)
return 32;
int n = 1;
if (i >>> 16 == 0) { n += 16; i <<= 16; }
if (i >>> 24 == 0) { n += 8; i <<= 8; }
if (i >>> 28 == 0) { n += 4; i <<= 4; }
if (i >>> 30 == 0) { n += 2; i <<= 2; }
n -= i >>> 31;
return n;
}
numberOfTrailingZeros
public static int numberOfTrailingZeros(int i)
返回指定的 int 值的二进制补码表示形式中最低(“最右边”)的为 1 的位后面的零位个数。如果指定值在它的二进制补码表示形式中没有为 1 的位,即它的值为零,则返回 32。
返回:
返回在指定 int 值的二进制补码表示形式中最低位(最右边)的 1 位之后零位的数量;否则,如果该值等于零,则返回 32。
从以下版本开始:
1.5
/**
* Returns the number of zero bits following the lowest-order ("rightmost")
* one-bit in the two's complement binary representation of the specified
* {@code int} value. Returns 32 if the specified value has no
* one-bits in its two's complement representation, in other words if it is
* equal to zero.
*
* @param i the value whose number of trailing zeros is to be computed
* @return the number of zero bits following the lowest-order ("rightmost")
* one-bit in the two's complement binary representation of the
* specified {@code int} value, or 32 if the value is equal
* to zero.
* @since 1.5
*/
public static int numberOfTrailingZeros(int i) {
// HD, Figure 5-14
int y;
if (i == 0) return 32;
int n = 31;
y = i <<16; if (y != 0) { n = n -16; i = y; }
y = i << 8; if (y != 0) { n = n - 8; i = y; }
y = i << 4; if (y != 0) { n = n - 4; i = y; }
y = i << 2; if (y != 0) { n = n - 2; i = y; }
return n - ((i << 1) >>> 31);
}
bitCount
public static int bitCount(int i)
返回指定 int 值的二进制补码表示形式的 1 位的数量。此函数有时用于人口普查。
返回:
返回指定 int 值的二进制补码表示形式的 1 位的数量。
从以下版本开始:
1.5
public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
rotateLeft
public static int rotateLeft(int i,
int distance)
返回根据指定的位数循环左移指定的 int 值的二进制补码表示形式而得到的值。(位是从左边(即高位)移出,从右边(即低位)再进入)
注意,使用负距离的左循环等同于右循环:rotateLeft(val, -distance) = = rotateRight(val, distance)。还要注意的是,以 32 的任何倍数进行的循环都是无操作指令,因此,即使距离为负,除了最后五位外,其余所有循环距离都可以忽略:rotateLeft(val, distance) == rotateLeft(val, distance & 0x1F)。
返回:
返回根据指定的位数循环左移指定的 int 值的二进制补码表示形式而得到的值。
从以下版本开始:
1.5
/**
* Returns the value obtained by rotating the two's complement binary
* representation of the specified {@code int} value left by the
* specified number of bits. (Bits shifted out of the left hand, or
* high-order, side reenter on the right, or low-order.)
*
* <p>Note that left rotation with a negative distance is equivalent to
* right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
* distance)}. Note also that rotation by any multiple of 32 is a
* no-op, so all but the last five bits of the rotation distance can be
* ignored, even if the distance is negative: {@code rotateLeft(val,
* distance) == rotateLeft(val, distance & 0x1F)}.
*
* @param i the value whose bits are to be rotated left
* @param distance the number of bit positions to rotate left
* @return the value obtained by rotating the two's complement binary
* representation of the specified {@code int} value left by the
* specified number of bits.
* @since 1.5
*/
public static int rotateLeft(int i, int distance) {
return (i << distance) | (i >>> -distance);
}
rotateRight
public static int rotateRight(int i,
int distance)
返回根据指定的位数循环右移指定的 int 值的二进制补码表示形式而得到的值。(位是从右边(即低位)移出,从左边(即高位)再进入)
注意,使用负距离的右循环等同于左循环:rotateRight(val, -distance) = = rotateLeft(val, distance)。还要注意的是,以 32 的任何倍数进行的循环都是无操作指令,因此,即使距离为负,除了最后五位外,其余所有循环距离都可以忽略:rotateRight(val, distance) == rotateRight(val, distance & 0x1F)。
返回:
返回根据指定的位数循环右移指定的 int 值的二进制补码表示形式而得到的值。
从以下版本开始:
1.5
/**
* Returns the value obtained by rotating the two's complement binary
* representation of the specified {@code int} value right by the
* specified number of bits. (Bits shifted out of the right hand, or
* low-order, side reenter on the left, or high-order.)
*
* <p>Note that right rotation with a negative distance is equivalent to
* left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
* distance)}. Note also that rotation by any multiple of 32 is a
* no-op, so all but the last five bits of the rotation distance can be
* ignored, even if the distance is negative: {@code rotateRight(val,
* distance) == rotateRight(val, distance & 0x1F)}.
*
* @param i the value whose bits are to be rotated right
* @param distance the number of bit positions to rotate right
* @return the value obtained by rotating the two's complement binary
* representation of the specified {@code int} value right by the
* specified number of bits.
* @since 1.5
*/
public static int rotateRight(int i, int distance) {
return (i >>> distance) | (i << -distance);
}
reverse
public static int reverse(int i)
返回通过反转指定 int 值的二进制补码表示形式中位的顺序而获得的值。
返回:
返回通过反转指定 int 值中位的顺序而获得的值。
从以下版本开始:
1.5
/**
* Returns the value obtained by reversing the order of the bits in the
* two's complement binary representation of the specified {@code int}
* value.
*
* @param i the value to be reversed
* @return the value obtained by reversing order of the bits in the
* specified {@code int} value.
* @since 1.5
*/
public static int reverse(int i) {
// HD, Figure 7-1
i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
i = (i << 24) | ((i & 0xff00) << 8) |
((i >>> 8) & 0xff00) | (i >>> 24);
return i;
}
signum
public static int signum(int i)
返回指定 int 值的符号函数。(如果指定值为负,则返回 -1;如果指定值为零,则返回 0;如果指定的值为正,则返回 1。)
返回:
返回指定 int 值的符号函数。
从以下版本开始:
1.5
/**
* Returns the signum function of the specified {@code int} value. (The
* return value is -1 if the specified value is negative; 0 if the
* specified value is zero; and 1 if the specified value is positive.)
*
* @param i the value whose signum is to be computed
* @return the signum function of the specified {@code int} value.
* @since 1.5
*/
public static int signum(int i) {
// HD, Section 2-7
return (i >> 31) | (-i >>> 31);
}
reverseBytes
public static int reverseBytes(int i)返回通过反转指定 int 值的二进制补码表示形式中字节的顺序而获得的值。
返回:
返回通过反转指定 int 值的字节而获得的值。
从以下版本开始:
1.5
/**
* Returns the value obtained by reversing the order of the bytes in the
* two's complement representation of the specified {@code int} value.
*
* @param i the value whose bytes are to be reversed
* @return the value obtained by reversing the bytes in the specified
* {@code int} value.
* @since 1.5
*/
public static int reverseBytes(int i) {
return ((i >>> 24) ) |
((i >> 8) & 0xFF00) |
((i << 8) & 0xFF0000) |
((i << 24));
}
测试
package testlang;
/**
* @author CSDN@日星月云
* @date 2022/10/24 22:32
*/
public class testInteger {
public static void main(String[] args) {
Integer i=new Integer(15);
System.out.println(i.toString());//15
// 传入一个int参数i,返回其二进制最低位1的权值
System.out.println(Integer.lowestOneBit(i));//1
// 传入一个int参数i,返回其二进制最高位1的权值
System.out.println(Integer.highestOneBit(i));//8
//在指定 int 值的二进制补码表示形式中最高位(最左边)的 1 位之前,返回零位的数量
System.out.println(Integer.numberOfLeadingZeros(i));//28 32位,15=1111,4个一,前面还有28个零
//返回指定的 int 值的二进制补码表示形式中最低(“最右边”)的为 1 的位后面的零位个数。
System.out.println(Integer.numberOfTrailingZeros(i));//0 最右边为1后面的零的个数是0
//返回指定 int 值的二进制补码表示形式的 1 位的数量。此函数有时用于人口普查。
System.out.println(Integer.bitCount(i));//4 4个一
//返回根据指定的位数循环左移指定的 int 值的二进制补码表示形式而得到的值。(位是从左边(即高位)移出,从右边(即低位)再进入)
System.out.println(Integer.rotateLeft(i,1));//30 循环左移 11110 15*2=30
//返回根据指定的位数循环右移指定的 int 值的二进制补码表示形式而得到的值。(位是从右边(即低位)移出,从左边(即高位)再进入)
System.out.println(Integer.rotateRight(i,1));//-2147483641 循环右移 1(28个0)111 二进制补码
//原码是 -(28个1)001
System.out.println(Integer.toBinaryString(Integer.rotateRight(i,1)));//10000000000000000000000000000111
//返回通过反转指定 int 值的二进制补码表示形式中位的顺序而获得的值。
System.out.println(Integer.reverse(i));//-268435456 翻转 1111(28个0)二进制补码
//原码是 -001(28个0)
System.out.println(-Math.pow(2,28));//2.68435456E8
//返回指定 int 值的符号函数。(如果指定值为负,则返回 -1;如果指定值为零,则返回 0;如果指定的值为正,则返回 1。)
System.out.println(Integer.signum(i));//1
}
}
总结
关键词:
- IntegerCache
- 缓存[-128,127]
- XxxValue
- ValueOf
- highestOnbit:传入一个int参数i,返回其二进制最高位1的权值
- lowestOnbit:传入一个int参数i,返回其二进制最低位1的权值
- 位操作:HACKER’S DELIGHT[算法心得]笔记
最后
开源=为爱发电