欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java代码中可以优化性能的小细节

程序员文章站 2024-01-23 21:25:04
...

避免对boolean类型的判定
反例:

<figure class="highlight smalltalk"><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">if(<span class="comment">"a"</span>.equles(<span class="comment">"a"</span>)==<span class="keyword">true</span>)`{</span><br><span class="line">}</span><br></pre></td> </tr></table></figure>

正例:

<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&lt;<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;
}

<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>

if(a>=0)`{
return 0;
}else{
return 1;
}

<figure class="highlight zephir"><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></pre></td> <td class="code"><pre><span class="line"></span><br><span class="line">**快速操作一个超大<span class="keyword">List</span>的连续性数据**</span><br><span class="line"></span><br><span class="line">使用集合的`<span class="keyword">List</span>&lt;E&gt; subList(<span class="keyword">int</span> fromIndex,<span class="keyword">int</span> toIndex)`方法可以返回一个超大<span class="keyword">List</span>的一段连续性数据,此时对这个subList的所有操作比如`clear()`等操作都会影响到原<span class="keyword">List</span></span><br><span class="line"></span><br><span class="line">**当一个方法的返回值是<span class="keyword">List</span>的时候请返回一个size为<span class="number">0</span>的<span class="keyword">list</span>而不是<span class="keyword">null</span>从而避免空指针异常**</span><br><span class="line"></span><br><span class="line">**单个字符串连接时使用单引号代替双引号**</span><br><span class="line">反例:</span><br></pre></td> </tr></table></figure>

“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++){
}

<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>

for(int i=0,n=a.size();i<n;i++){
}

<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></pre></td> <td class="code"><pre><span class="line"></span><br><span class="line">*<span class="strong">*减少不必要的需要循环的次数*</span><span class="strong">*</span></span><br><span class="line"><span class="strong">反例:</span></span><br></pre></td> </tr></table></figure>

int index=-1;
for(int i=0,n=a.size();i<n;i++){
if(a.get(i)==3){
index=i;
}
}

<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>

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)
相关标签: 性能调优