项目代码优化(一)

本文主要探讨了四个Java代码优化的方面:正确使用Integer以提高效率,执行除法时如何保留精度,避免空指针异常的判断,以及处理未捕获的异常。建议在编程时遵循这些最佳实践,以提升代码的性能和稳定性。
摘要由CSDN通过智能技术生成

a.正确使用Integer

  • 场景
	
	Map<String,Object> pp = new HashMap<>();
	pp.put("subscriberCode", sn);
	pp.put("currentTime", UTCTimeUtil.getUTCDateTimeStr());
	pp.put("durationBalance", new Integer(0));
	
  • 解释
  • 英文

com.snt.crm.business.service.impl.SubTrafficCDRServiceImpl.saveVsatCdr(JSONObject) invokes inefficient new Integer(int) constructor; use Integer.valueOf(int) instead

	Performance - Method invokes inefficient Number constructor; use static valueOf instead
	Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) allows caching of values to be done by the compiler, class library, or JVM. Using of cached values avoids object allocation and the code will be faster.
Values between -128 and 127 are guaranteed to have corresponding cached instances and using valueOf is approximately 3.5 times faster
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曾卫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值