Java代码中可以优化性能的小细节
程序员文章站
2024-01-23 21:25:04
...
避免对boolean类型的判定
反例:
正例:
<figure class="highlight stylus"><table><tr> <td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td> <td class="code"><pre><span class="line"><span class="function"><span class="title">if</span><span class="params">(Objects.equles(<span class="string">"a"</span>,<span class="string">"a"</span>)</span></span>)`{</span><br><span class="line">}</span><br></pre></td> </tr></table></figure>**多用条件判断符 **
反例:
<figure class="highlight coffeescript"><table><tr> <td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td> <td class="code"><pre><span class="line"><span class="keyword">if</span>(a<<span class="number">0</span>)`<span class="javascript">{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">return</span> <span class="number">0</span>;</span></span><br><span class="line"><span class="javascript">}<span class="keyword">else</span>{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">return</span> <span class="number">1</span>;</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"></span>``<span class="javascript"></span>` </span><br><span class="line">正例:</span><br></pre></td> </tr></table></figure>a<0?0:1;
<figure class="highlight asciidoc"><table><tr> <td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td> <td class="code"><pre><span class="line"></span><br><span class="line">*<span class="strong">*避免使用instanceof、避免把子类向父类转型*</span><span class="strong">*</span></span><br><span class="line"><span class="strong">掌握好多态即可</span></span><br><span class="line"><span class="strong"></span></span><br><span class="line"><span class="strong"></span></span><br><span class="line"><span class="strong"></span></span><br><span class="line"><span class="strong">*</span><span class="strong">*建议多使用局部变量*</span><span class="strong">*</span></span><br><span class="line"><span class="strong">局部变量保存在栈中,占用的内存随着方法的执行完毕而被释放</span></span><br><span class="line"><span class="strong"></span></span><br><span class="line"><span class="strong">*</span><span class="strong">*多使用位运算代替算术运算*</span><span class="strong">*</span></span><br><span class="line"><span class="strong">就是可读性差点</span></span><br><span class="line"><span class="strong"></span></span><br><span class="line"><span class="strong">*</span><span class="strong">*一维数组和二维数组的恩怨情仇*</span><span class="strong">*</span></span><br><span class="line"><span class="strong">保存同样的数据使用一维数组和二维数组的区别是前者以时间换空间后者是以空间换时间</span></span><br><span class="line"><span class="strong"></span></span><br><span class="line"><span class="strong">*</span><span class="strong">*不要经常使用取反操作符*</span><span class="strong">*</span></span><br><span class="line"><span class="strong"></span></span><br><span class="line"><span class="strong">反例:</span></span><br></pre></td> </tr></table></figure>if(!a<0)`{
return 0;
}else{
return 1;
}
if(a>=0)`{
return 0;
}else{
return 1;
}
“abc”+“d”;
<figure class="highlight plain"><table><tr> <td class="gutter"><pre><span class="line">1</span><br></pre></td> <td class="code"><pre><span class="line">正例:</span><br></pre></td> </tr></table></figure>“abc”+‘d’;
<figure class="highlight asciidoc"><table><tr> <td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td> <td class="code"><pre><span class="line">*<span class="strong">*大量字符串拼接时使用StringBuffer代替+*</span><span class="strong">*</span></span><br><span class="line"><span class="strong"></span></span><br><span class="line"><span class="strong">*</span><span class="strong">*切割字符串的时候使用StringTokenizer代替split*</span><span class="strong">*</span></span><br><span class="line"><span class="strong"></span></span><br><span class="line"><span class="strong">*</span><span class="strong">*提前计算出需要循环的次数*</span><span class="strong">*</span></span><br><span class="line"><span class="strong">反例:</span></span><br></pre></td> </tr></table></figure>for(int i=0i<a.size();;i++){
}
for(int i=0,n=a.size();i<n;i++){
}
int index=-1;
for(int i=0,n=a.size();i<n;i++){
if(a.get(i)==3){
index=i;
}
}
int index=-1;
for(int i=0,n=a.size();i<n;i++){
if(a.get(i)==3){
index=i;
break;
}
}
**不要在循环中改变数组/List的大小**
**使用`System.arrayCopy`进行复制数组**
**使用Buffer进行IO操作**
**释放资源的代码要放在finally代码块中**
**多用java.util.Obects工具类**
<h4 style="color:red">推荐阅读</h4>
1. [SpringCloud学习系列汇总](https://mp.weixin.qq.com/s/TL4w0u6QA-wFx19IH9Xs6A)
2. [为什么一线大厂面试必问redis,有啥好问的?](https://mp.weixin.qq.com/s/NSojCEA-lF8ocR1XeuXbsg)
3. [多线程面试必备基础知识汇总](https://mp.weixin.qq.com/s/c3ngvdAc6lAs4d1sIdg16Q/)
4. [Java集合源码分析汇总-JDK1.8](https://mp.weixin.qq.com/s/UAGihOEUPmFnwrgkSJZqpQ)
5. [Linux常用命令速查-汇总篇](https://mp.weixin.qq.com/s/WG4ZWfId-MRslWtcDtx0aA)
6. [JVM系列文章汇总](https://mp.weixin.qq.com/s/uDftoMTwJqk7H9bfQaa0sw)
7. [MySQL系列文章汇总](https://mp.weixin.qq.com/s/SUV1DRSFhOIksj9C_qC7aA)
7. [RabbitMQ系列文章汇总](https://mp.weixin.qq.com/s/rDqhdBk879neFM_CZzK9Yw)
<h4 style="color:red">博客所有文章首发于公众号《Java学习录》转载请保留
扫码关注公众号即可领取2000GJava学习资源 </h4>
![1](https://shiyujun.cn/img/gongzhonghao.jpg)
上一篇: 非常实用的10个PHP高级应用技巧
下一篇: 数据结构与算法-双向链表(js实现)
推荐阅读
-
实用 | 35个可以提高千倍效率的Java代码小技巧 java
-
实用 | 35个可以提高千倍效率的Java代码小技巧 java
-
小码农的代码(四)----------JAVA中Timer定时器与Spring定时任务
-
Java性能优化的十条小技巧
-
所谓的网页爬虫用java代码来实现,此代码适合在maven项目中使用中使用,因为,代码中的类所对应的依赖可以让maven下载。
-
从代码的角度谈如何优化JAVA代码以提高性能【初、中级程序员必看】
-
推荐风轻扬:Java 6中的性能优化
-
SpringMVC 获取国际化信息(JSTL view,java代码中,超链接可以选择的方式)
-
js中闭包性能优化的代码解析
-
Cordova插件中JavaScript代码与Java的交互细节介绍 CordovaJavaScriptSAPUI5