ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

StringBuffer和 StringBuilder和包装类

2020-11-07 08:33:22  阅读:219  来源: 互联网

标签:包装 StringBuffer 127 high int static StringBuilder Integer IntegerCache


StringBuffer和 StringBuilder

  • StringBuffer :可变长字符串,运行效率慢、线程安全

    1. append()方法 追加
    2. insert(int, String) 添加,可以在任意地方
    3. replace(int start, int end, String) 指定位置替换,含头不含尾
    4. delete(int start, int end) 删除
    5. reverse()
  • StringBuilder:可变长字符串,运行效率快、线程不安全

    方法和上面一样

wrapper class

  • Byte
  • Short
  • Integer
  • Long
  • Character
  • Float
  • Double
  • Boolean

装箱:基本转对象

拆箱:对象转基本

编译器会自动转换,完成自动装箱拆箱

分析Integer

  • public int intValue()
    

    将对象转化为int值

  • public static Integer valueOf(int i)
    

    将int值转换为Integer对象

  • public static int parseInt(String s)
    

    字符串转换为Int值

  • IntegerCache

    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() {}
        }
    

    ​ 会初始化-128到127,通过valueOf创建在这个范围内是相等的(复用)

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

    但是如果是用Integer构造方法创造,即使在这个范围内,也是不等的

标签:包装,StringBuffer,127,high,int,static,StringBuilder,Integer,IntegerCache
来源: https://www.cnblogs.com/zxy-GO/p/13939758.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有