IDEA代码里的黄色提示

#cec726

1:String concatenation as argument to 'StringBuilder.append()' 

详细提示

String concatenation as argument to 'StringBuilder.append()' call less... (Ctrl+F1)
Inspection info: Reports String concatenation used as the argument to StringBuffer.append(), StringBuilder.append() or Appendable.append(). Such calls may profitably be turned into chained append calls on the existing StringBuffer/Builder/Appendable, saving the cost of an extra StringBuffer/Builder allocation.
This inspection ignores compile time evaluated String concatenations, which when converted to chained append calls would only worsen performance.

解释

append方法就是为了解决String += String的效率问题的,而上面代码在append方法里使用了字符串连接,可能降低性能。解决提示:不要在append方法里写字符串连接代码。

 

 

2:C-style array declaration of local variable 'arr' less... 

详细提示

C-style array declaration of local variable 'arr' less... (Ctrl+F1)
Inspection info: Reports array declarations made using C-style syntax, with the array indicator brackets positioned after the variable name or after the method parameter list. For example:
public String process(String value[])[] {
return value;
}
Most code styles prefer Java-style array declarations, with the array indicator brackets attached to the type name.

解释

数组对象,把中括号放在变量后面是C语言的风格,而Java习惯放在变量类型后面

 

 

3:'for' loop replaceable with 'foreach' less... 

详细提示

'for' loop replaceable with 'foreach' less... (Ctrl+F1)
Inspection info: Reports for loops which iterate over collections or arrays, and can be replaced with the foreach iteration syntax, available in Java 5 and newer.

解释

循环遍历集合或数组,可以替换为foreach迭代语法

 

 

4:Local variable 's' is redundant less...

详细提示

Local variable 's' is redundant less... (Ctrl+F1)
Inspection info: Reports unnecessary local variables, which add nothing to the comprehensibility of a method. Variables caught include local variables which are immediately returned, local variables that are immediately assigned to another variable and then not used, and local variables which always have the same value as another local variable or parameter.

解释

定义的变量名称是冗余的

 

 

5:Field injection is not recommended less...

详细提示

Field injection is not recommended less... (Ctrl+F1)
Inspection info: Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".

解释

Spring团队建议:“始终在bean中使用基于构造函数的依赖注入

 

 

6:Found duplicate code

鼠标放在重复代码块,Alt+Enter

查看其它重复代码,可以做比较

 

7:Call to 'toArray()' with pre-sized array argument 'new String[prdCodeList.size()]'

 

详细提示

Call to 'toArray()' with pre-sized array argument 'new String[prdCodeList.size()]' less... (Ctrl+F1)
Inspection info: There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).
In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.
This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).

解释

有两种样式将集合转换成数组:

1. c.toArray(new String[c.size()])

2. c.toArray(new String[0])

在旧的Java版本中,建议使用预大小的数组,因为创建适当大小的数组所必需的反射调用非常慢。

JDK6开始,与预设置大小的版本相比,空数组版本的性能相同,有时甚至更好。现在推荐使用空数组版本

 

8:'if' statement can be simplified

 

 详细提示

'if' statement can be simplified less... (Ctrl+F1)
Inspection info: Reports if statements which can be simplified to single assignment, return or assert statements.
For example:
  if (foo()) {
    return true;
  } else {
    return false;
  }
can be simplified to
  return foo();

解释

因为你if语句里的判断结果就是Boolean类型的,所以你不必脱了裤子放屁。【JDK源码和Spring源码,这种代码随处可见】

9:Can be replaced with single 'Map.putIfAbsent' method call

 

 详细提示

Can be replaced with single 'Map.putIfAbsent' method call less... (Ctrl+F1)
Inspection info: Reports calls to Map.get() which could be replaced with getOrDefault(), computeIfAbsent() or putIfAbsent().
Map.getOrDefault method could be used to replace the code like this:
  String val = map.containsKey(key) ? map.get(key) : "none";
Map.computeIfAbsent method could be used to replace the code like this:
  List<String> list = map.get(key);
  if (list == null) {
    list = new ArrayList<>();
    map.put(key, list);
  }
Map.putIfAbsent method could be used to replace the code like this:
  String val = map.get(key);
  if (val == null) map.put(key, newVal);
Map.merge method could be used to replace the code like this:
  Integer val = map.get(key);
  if (val == null) map.put(key, 1);
  else map.put(key, val + 1);
Note that replacement with computeIfAbsent() or merge() may work incorrectly for some Map implementations if the code extracted to lambda expression modifies the same Map. By default, warning is not issued if this code may have side effects. If desired, use the last checkbox to issue warning always.

解释

传统的操作可以用Map提供的新方法替代,比如上面判断不存在才添加,可以写成 map.putIfAbsent("A", "AAA");

10. Diamond types are not supported at language level '5'

 

出现

11. StandardCharsets.UTF_8 can be used instead

 

 详细提示

StandardCharsets.UTF_8 can be used instead less... (Ctrl+F1)
Inspection info: Reports methods and constructors where constant charset String literal is used (like "UTF-8") which could be replaced with a predefined Charset object like StandardCharsets.UTF_8. This may work a little bit faster, because charset lookup becomes unnecessary. Also catching UnsupportedEncodingException may become unnecessary as well. In this case the catch block will be removed automatically.

解释

字符串设置字符集,可以更改为预定义的常量,比如 java.nio.charset.StandardCharsets.UTF_8

 

posted @ 2020-07-08 16:04  露娜妹  阅读(18232)  评论(1编辑  收藏  举报