int 是 Java 八大原始类型之一,是 Java 语言中为数不多不是对象的东西,Integer 是 int 的包装类,里面使用了一个 int 类型的变量来存储数据,提供了一些整数之间的常用操作,常规性的介绍就这么一点,程序员不喜欢说,程序员就喜欢源码,我们还是来看源码吧

  1. * @author Lee Boynton
  2. * @author Arthur van Hoff
  3. * @author Josh Bloch
  4. * @author Joseph D. Darcy
  5. * @since JDK1.0
  6. */
  7. public final class Integer extends Number implements Comparable<Integer> {
  8. /**
  9. * A constant holding the minimum value an {@code int} can
  10. * have, -2<sup>31</sup>.
  11. */
  12. @Native public static final int MIN_VALUE = 0x80000000;
  13. /**
  14. * A constant holding the maximum value an {@code int} can
  15. * have, 2<sup>31</sup>-1.
  16. */
  17. @Native public static final int MAX_VALUE = 0x7fffffff;
  18. /**
  19. * The value of the {@code Integer}.
  20. *
  21. * @serial
  22. */
  23. private final int value;
  24. /**
  25. * Constructs a newly allocated {@code Integer} object that
  26. * represents the specified {@code int} value.
  27. *
  28. * @param value the value to be represented by the
  29. * {@code Integer} object.
  30. */
  31. public Integer(int value) {
  32. this.value = value;
  33. }
  34. /**
  35. * Constructs a newly allocated {@code Integer} object that
  36. * represents the {@code int} value indicated by the
  37. * {@code String} parameter. The string is converted to an
  38. * {@code int} value in exactly the manner used by the
  39. * {@code parseInt} method for radix 10.
  40. *
  41. * @param s the {@code String} to be converted to an
  42. * {@code Integer}.
  43. * @exception NumberFormatException if the {@code String} does not
  44. * contain a parsable integer.
  45. * @see java.lang.Integer#parseInt(java.lang.String, int)
  46. */
  47. public Integer(String s) throws NumberFormatException {
  48. this.value = parseInt(s, 10);
  49. }

上面这段源码是我截取出来的,在 Integer 类中,这些代码不是连在一起的,把他们放在一起,那是因为我想说明点事情,我们仔细看看这段代码,Integer 类是被 final ,这说明了什么?用于存放变量的 value 也被 private final 修饰,这又说明了什么?看着这些是不是有点熟悉呢? 没错,String 对象也是这样的,这说明 Integer 对象也是不可变的,所以以后如果被问到 Integer 对象是不是不可变对象时,记得回答是喔。为什么 Integer 对象也会设计成不可变对象呢?其实我也不知道,我没有从文档中找到答案,但是在杨晓峰老师的文章中看到过有关说明,杨晓峰老师说 Integer 类设计成不可变跟 getInteger() 方法有关系,getInteger()方法的源码如下:

  1. public static Integer getInteger(String nm, Integer val) {
  2. String v = null;
  3. try {
  4. v = System.getProperty(nm);
  5. } catch (IllegalArgumentException | NullPointerException e) {
  6. }
  7. if (v != null) {
  8. try {
  9. return Integer.decode(v);
  10. } catch (NumberFormatException e) {
  11. }
  12. }
  13. return val;
  14. }

getInteger() 方法是用来获取系统属性的,我们通过属性来设置服务器的某个服务器的端口,如果 Integer 可变的话,那么我们就能够轻易的改变这个属性的值,这会使得我们的产品存在安全风险。

上面我们我么简单的聊了一下 Integer 类的实现,聊到 int 与 Integer,自然就少不了自动装箱和自动拆箱。

自动装箱和拆箱是从 JDK 1.5 开始引进的功能,它是一种语法糖,Java 可以根据上下文,自动的在原始类型和包装类型中进行转换,简单的来说就是 Java 平台保证了不同的写法通过编译之后会产生相同的字节码,保证了运行时是等价的。自动装箱和拆箱极大地简化了相关编程。

自动装箱:将原始类型转化为包装类型的过程

比如将 int 类型转换成 integer 类型,这就是原始类型到包装类型的转变,在编译的时候,编译器就会帮我们做自动转换,这个过程对我们程序员来说是无感知的,例如这段给 Integer 对象赋值的代码:

  1. Integer x = 3000;

这段代码经过编译器之后,会转换成下面这段代码:

  1. Integer x = Integer.valueOf(3000);

这就是自动装箱过程,这个过程你是不知道的,所以它才叫自动装箱,在自动装箱的过程中使用到了 valueOf() 方法,来看看 JDK 中这个方法的源码:

  1. public static Integer valueOf(int i) {
  2. if (i >= IntegerCache.low && i <= IntegerCache.high)
  3. return IntegerCache.cache[i + (-IntegerCache.low)];
  4. return new Integer(i);
  5. }

这个方法里,前面先进行了一个缓存判断,如果你不知道的话,先忽略掉它,最后返回了 new Integer(i) 对象引用,这个方法就是帮你去调用了 Integer 类的构造器。这就是自动装箱。

自动拆箱:将包装类型转换成原始类型的过程

将 Integer 类型转换为 Int 类型,这是一个包装类型转成成原始类型的过程,在这个过程中就会涉及到自动拆箱。来看看这段代码(这是一段很操蛋的代码,实际中应该没人这样写):

  1. Integer mm = 1000;
  2. int mmm = mm;

在编译的时候,这段代码会被编译器编译成下面这段代码:

  1. Integer mm = Integer.valueOf(1000);
  2. int mmm = mm.intValue();

主要看int mmm = mm.intValue();这行代码,这行代码跟我们写的不一样了,使用到了一个 intValue() 方法,来看看 Integer 类中 intValue() 方法的源码:

  1. /**
  2. * Returns the value of this {@code Integer} as an
  3. * {@code int}.
  4. */
  5. public int intValue() {
  6. return value;
  7. }

这个方法的作用就是把 Integer 对象中用来存储值的 value 变量返回了,这就是自动拆箱,好了,关于自动装箱和自动拆箱我们都了解了,还记得自动装箱过程中涉及到的缓存吗?接下来我们一起了解一下。

在自动装箱的 valueOf() 方法中,我们看到了有一个缓存判断的操作,是的,Integer 类中有缓存池,会将使用频繁的值缓存起来,以便提高系统的使用性能,在自动装箱的过程中,会先判断该值是否存在缓存池中,如果存在直接从缓存池中取出引用返回,如果不存在则调用构造函数构造对象。缓存是自动装箱操作独享的,直接通过构造函数构造出来的 Integer 对象即使值在缓存范围内,也不会使用到缓存池。在 Integer 类中,使用了一个内部类来实现缓存,这个内部类叫做 IntegerCache,IntegerCache 类的源代码如下:

  1. /**
  2. * Cache to support the object identity semantics of autoboxing for values between
  3. * -128 and 127 (inclusive) as required by JLS.
  4. *
  5. * The cache is initialized on first usage. The size of the cache
  6. * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
  7. * During VM initialization, java.lang.Integer.IntegerCache.high property
  8. * may be set and saved in the private system properties in the
  9. * sun.misc.VM class.
  10. */
  11. private static class IntegerCache {
  12. static final int low = -128;
  13. static final int high;
  14. static final Integer cache[];
  15. static {
  16. // high value may be configured by property
  17. int h = 127;
  18. String integerCacheHighPropValue =
  19. sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
  20. if (integerCacheHighPropValue != null) {
  21. try {
  22. int i = parseInt(integerCacheHighPropValue);
  23. i = Math.max(i, 127);
  24. // Maximum array size is Integer.MAX_VALUE
  25. h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
  26. } catch( NumberFormatException nfe) {
  27. // If the property cannot be parsed into an int, ignore it.
  28. }
  29. }
  30. high = h;
  31. cache = new Integer[(high - low) + 1];
  32. int j = low;
  33. for(int k = 0; k < cache.length; k++)
  34. cache[k] = new Integer(j++);
  35. // range [-128, 127] must be interned (JLS7 5.1.7)
  36. assert IntegerCache.high >= 127;
  37. }
  38. private IntegerCache() {}
  39. }

从源码和 Java 注释中我们可以看出 IntegerCache 的缓存默认值范围 -128 ~ 127 。但是我们也可以在启动时通过 JVM 命令来设置缓存范围的最大值,只需要在启动时添加 -XX:AutoBoxCacheMax=
参数就可以了,但是记得这个 size 可不要乱设置,需要全方位考虑,比如你设置成 10 万,那么这 10 万个数都会在启动刚启动时就添加到内存中,想想这会占用你多少内存?这样做就得不偿失了,Java 公司设置成 -128 ~ 127 是有道理的,发现大部分人使用的值都在 -128 ~ 127 之间,这些值占用的内存比较少,性能上比通过构造函数构造对象要好不少。如何你使用的 Integer 的值在缓存范围的话,就用 Integer i = value 的形式构建对象,如果你的值不在缓存范围内,则使用 Integer i = new Integer(value) 的形式构建 Integer 对象,避免自动装箱的过程。最后我们来看一下 Integer 对象比较常用的方法 parseInt 方法

parseInt() 方法的作用是用来将整数型的字符串转换成整数,parseInt 方法需要和 valueOf 方法区分开来,有不少人会问这两方法有什么区别,最后度会返回 int 类型,都能将整数型的字符串转换成整数型,比如这段代码

  1. System.out.println(Integer.parseInt("+12"));
  2. System.out.println(Integer.valueOf("+12"));

最后都会输出 12 ,输出的结果相同是因为 valueOf 方法使用中会调用 parseInt 方法将整数型字符转换为整数,并且会在内存中创建一个值为 12 的 Integer 对象,然后返回这个对象引用。而 parseInt 方法只会帮你将整数型字符转换为整数,不会额外的创建对象。所以它们两得到相同的结果纯属是巧合。一起瞅瞅 parseInt 源代码:

  1. public static int parseInt(String s, int radix)
  2. throws NumberFormatException
  3. {
  4. /*
  5. * WARNING: This method may be invoked early during VM initialization
  6. * before IntegerCache is initialized. Care must be taken to not use
  7. * the valueOf method.
  8. */
  9. if (s == null) {
  10. throw new NumberFormatException("null");
  11. }
  12. if (radix < Character.MIN_RADIX) {
  13. throw new NumberFormatException("radix " + radix +
  14. " less than Character.MIN_RADIX");
  15. }
  16. if (radix > Character.MAX_RADIX) {
  17. throw new NumberFormatException("radix " + radix +
  18. " greater than Character.MAX_RADIX");
  19. }
  20. int result = 0;
  21. boolean negative = false;
  22. int i = 0, len = s.length();
  23. int limit = -Integer.MAX_VALUE;
  24. int multmin;
  25. int digit;
  26. if (len > 0) {
  27. char firstChar = s.charAt(0);
  28. if (firstChar < '0') { // Possible leading "+" or "-"
  29. if (firstChar == '-') {
  30. negative = true;
  31. limit = Integer.MIN_VALUE;
  32. } else if (firstChar != '+')
  33. throw NumberFormatException.forInputString(s);
  34. if (len == 1) // Cannot have lone "+" or "-"
  35. throw NumberFormatException.forInputString(s);
  36. i++;
  37. }
  38. multmin = limit / radix;
  39. while (i < len) {
  40. // Accumulating negatively avoids surprises near MAX_VALUE
  41. digit = Character.digit(s.charAt(i++),radix);
  42. if (digit < 0) {
  43. throw NumberFormatException.forInputString(s);
  44. }
  45. if (result < multmin) {
  46. throw NumberFormatException.forInputString(s);
  47. }
  48. result *= radix;
  49. if (result < limit + digit) {
  50. throw NumberFormatException.forInputString(s);
  51. }
  52. result -= digit;
  53. }
  54. } else {
  55. throw NumberFormatException.forInputString(s);
  56. }
  57. return negative ? result : -result;
  58. }

在调用 parseInt 方法时,我们可以传入一个 radix 变量,用来告诉它使用什么进制来进行转换,默认使用的是十进制。

文章不足之处,望大家多多指点,共同学习,共同进步

打个小广告,欢迎扫码关注微信公众号:「平头哥的技术博文」,一起进步吧。
平头哥的技术博文

版权声明:本文为jamaler原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jamaler/p/11605945.html