当前位置: 首页 > wzjs >正文

网页制作网站制作WordPress大前端5

网页制作网站制作,WordPress大前端5,代做seo排名,减少WordPress跳转Java Integer包装类缓存机制详解 问题引入 在解决力扣第76题「最小覆盖子串」时&#xff0c;我使用了Map<Character, Integer>来记录字符串中各字符的出现次数。在比较两个字符串各自字符出现次数时&#xff0c;最初我使用了""操作符进行比较&#xff0c;但无法…

Java Integer包装类缓存机制详解

问题引入

在解决力扣第76题「最小覆盖子串」时,我使用了Map<Character, Integer>来记录字符串中各字符的出现次数。在比较两个字符串各自字符出现次数时,最初我使用了"=="操作符进行比较,但无法通过所有测试用例。后来改用equals()方法进行比较,最终成功通过了。

问题代码:

if(tMap.containsKey(s.charAt(right)) && sMap.get(s.charAt(right)) == tMap.get(s.charAt(right))){// 逻辑处理
}

修正后的代码:

if(tMap.containsKey(s.charAt(right)) && sMap.get(s.charAt(right)).equals(tMap.get(s.charAt(right)))){// 逻辑处理
}

包装类缓存机制

基本概念

这个问题的根本原因在于Map中存储的值类型是Integer包装类,而非基本类型int

包装类是Java对基本数据类型的封装。当包装类被加载到内存时,JVM会为其创建一个静态内部缓存类,该缓存保存在堆内存中。对于Integer类型,当数值在-128到127之间时,会直接使用缓存中的对象,此时==equals()的效果相同。

但当数值超出这个范围时,由于==比较的是对象引用而非对象值,就会出现相同数值但引用不同的情况,可能导致程序逻辑错误。

源码分析

以下是Integer类内部关于缓存的源码:

/*** 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 {// 获取JVM启动时的参数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);// 缓冲区需要表示负数,所以在设置int整数最大值的情况下,要去除负数和0的个数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() {}
}

自动装箱与缓存

Java中的自动装箱和自动拆箱机制使得包装类与基本数据类型之间的转换变得非常便捷。自动装箱会利用缓存机制,因为底层调用的是Integer.valueOf(int a)方法。

重要区别:

  • Integer.valueOf()方法:会使用缓存,对于-128到127范围内的数值,返回缓存中的对象
  • Integer.parseInt()方法:不使用缓存,每次都会创建新的Integer对象

示例代码:

Integer a = 127;
Integer b = 127;
System.out.println(a == b); // true,使用缓存Integer c = 128;
Integer d = 128;
System.out.println(c == d); // false,超出缓存范围Integer e = Integer.valueOf(100);
Integer f = Integer.valueOf(100);
System.out.println(e == f); // true,使用缓存

其他包装类的缓存机制

在Java中,除了FloatDouble之外,其他基本数据类型的包装类都有缓存机制:

基本数据类型包装类型缓存范围
byteByte-128 ~ 127
shortShort-128 ~ 127
intInteger-128 ~ 127
longLong-128 ~ 127
charCharacter0 ~ 127
booleanBooleantrue, false
floatFloat无缓存
doubleDouble无缓存

最佳实践

  1. 比较包装类对象值时,始终使用equals()方法,避免因缓存机制导致的意外行为
  2. 了解自动装箱的缓存范围,在性能敏感的场景中合理利用缓存
  3. 避免过度依赖缓存机制,编写健壮的代码逻辑

总结

Java包装类的缓存机制是JVM的一项优化措施,旨在减少小范围整数对象的创建开销。理解这一机制有助于我们:

  • 避免在对象比较时出现逻辑错误
  • 更好地理解自动装箱和拆箱的底层原理
  • 在实际开发中编写更加健壮的代码

记住:在比较包装类对象时,使用equals()方法是最安全的选择!


文章转载自:

http://4lLvJKXK.bydpr.cn
http://ooKfS8Yz.bydpr.cn
http://d89RCaOt.bydpr.cn
http://rlj0QrP5.bydpr.cn
http://GWReg1oO.bydpr.cn
http://QpWDnN2v.bydpr.cn
http://CRghYNfz.bydpr.cn
http://ZF5y8br2.bydpr.cn
http://L0rGPXkC.bydpr.cn
http://eoRPGaql.bydpr.cn
http://bDoVycKB.bydpr.cn
http://6j7IpoQv.bydpr.cn
http://cH1sYOYC.bydpr.cn
http://hn72E4Uw.bydpr.cn
http://Rg9MhXBr.bydpr.cn
http://yVEqutCi.bydpr.cn
http://i67PPEnJ.bydpr.cn
http://3P4keBCh.bydpr.cn
http://yv07Etpu.bydpr.cn
http://LJmxvgag.bydpr.cn
http://lKVeE4qK.bydpr.cn
http://IgRT20vR.bydpr.cn
http://P731hxxQ.bydpr.cn
http://5LqI3XOv.bydpr.cn
http://DVSAomCU.bydpr.cn
http://vruvK7n2.bydpr.cn
http://PeiSihrS.bydpr.cn
http://bq9JRbiF.bydpr.cn
http://kYKIkU4N.bydpr.cn
http://8LB7DQKy.bydpr.cn
http://www.dtcms.com/wzjs/693583.html

相关文章:

  • 黄骅市官方网站vue新增页面
  • 网站开发实现总结建设钓鱼网站源码
  • 鞍山做网站哪家好cms自助建站系统
  • 安康创宇网站制作建设wordpress积分冻结
  • 网站打开慢怎么回事手工艺品网站建设侧胡顺
  • 学做网站书籍呼和浩特网站制作
  • 怎样做好网站推广网上做国外兼职网站
  • 广州推广seo优化方案数学2023版电子版
  • 在本地搭建多个网站怎么做百度网盘链接网站
  • 东莞做网站电话模板网站建设信息
  • api接口开发网站开发贸易网站有哪些
  • 网站主页设计素材短视频营销国内外研究现状
  • 动态Js文件 做网站标题网络设计基本原则
  • 免费做app的网站有哪些数字城市建设网站
  • 小学做试卷的网站wordpress 静态页面显示文章
  • 北京专业响应式网站建设wordpress导出工具栏
  • 开篇网站推广台州大型网站建设
  • 南通网站推广优化公司建筑设计研究生考试科目
  • 做公司中文网站需要注意什么tp框架做餐饮网站
  • 专门做投标书的网站网站建设设计费用
  • 建材行业网站建设免费红色ppt模板网站
  • pp视频在线观看免费大全下载seo是哪个英文的缩写
  • 一个app多少钱重庆seo排名方法
  • 在百度做推广送网站好吗服务器 多wordpress
  • 山东建设和城乡建设厅注册中心网站个人如何做公益网站
  • 江苏建设厅施工员证报名网站如何创建网站老鱼网
  • 万能浏览器手机版下载安装2022网站站内的seo怎么做
  • 网站开发流程及详解网页制作东莞
  • 如何设计酒店网站建设网站注册页面跳出怎么做
  • 做网站卖菜刀需要什么手续珠宝网站dedecms模版